Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-7610 Add an option to initialize empty components when all the pr… #944

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -711,4 +711,11 @@ public interface AvailableSettings {

String AUTO_SESSION_EVENTS_LISTENER = "hibernate.session.events.auto";

/**
* Enable instantiation of composite/embedded objects when all of its attribute values are {@code null}.
* The default (and historical) behavior is that a {@code null} reference will be used to represent the
* composite when all of its attributes are {@code null}.
*/
String CREATE_EMPTY_COMPOSITES_ENABLED = "hibernate.create_empty_composites.enabled";

}
Expand Up @@ -31,6 +31,8 @@

import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Property;
import org.hibernate.tuple.PropertyFactory;
Expand All @@ -56,6 +58,7 @@ public class ComponentMetamodel implements Serializable {
// cached for efficiency...
private final int propertySpan;
private final Map propertyIndexes = new HashMap();
private final boolean createEmptyCompositesEnabled;

// public ComponentMetamodel(Component component, SessionFactoryImplementor sessionFactory) {
public ComponentMetamodel(Component component) {
Expand All @@ -82,6 +85,12 @@ public ComponentMetamodel(Component component) {
entityMode,
component
) : componentTuplizerFactory.constructTuplizer( tuplizerClassName, component );

this.createEmptyCompositesEnabled = ConfigurationHelper.getBoolean(
Environment.CREATE_EMPTY_COMPOSITES_ENABLED,
component.getMappings().getConfigurationProperties(),
false
);
}

public boolean isKey() {
Expand Down Expand Up @@ -123,4 +132,8 @@ public ComponentTuplizer getComponentTuplizer() {
return componentTuplizer;
}

public boolean isCreateEmptyCompositesEnabled() {
return createEmptyCompositesEnabled;
}

}
Expand Up @@ -67,6 +67,7 @@ public class ComponentType extends AbstractType implements CompositeType, Proced
private final FetchMode[] joinedFetch;
private final boolean isKey;
private boolean hasNotNullProperty;
private final boolean createEmptyCompositesEnabled;

protected final EntityMode entityMode;
protected final ComponentTuplizer componentTuplizer;
Expand Down Expand Up @@ -96,6 +97,7 @@ public ComponentType(TypeFactory.TypeScope typeScope, ComponentMetamodel metamod

this.entityMode = metamodel.getEntityMode();
this.componentTuplizer = metamodel.getComponentTuplizer();
this.createEmptyCompositesEnabled = metamodel.isCreateEmptyCompositesEnabled();
}

public boolean isKey() {
Expand Down Expand Up @@ -668,6 +670,9 @@ public Object resolve(Object value, SessionImplementor session, Object owner)
setPropertyValues( result, resolvedValues, entityMode );
return result;
}
else if ( isCreateEmptyCompositesEnabled() ) {
return instantiate( owner, session );
}
else {
return null;
}
Expand Down Expand Up @@ -813,4 +818,8 @@ public Object extract(CallableStatement statement, String[] paramNames, SessionI
public boolean hasNotNullProperty() {
return hasNotNullProperty;
}

private boolean isCreateEmptyCompositesEnabled() {
return createEmptyCompositesEnabled;
}
}
@@ -0,0 +1,28 @@
package org.hibernate.test.component.empty;

import javax.persistence.Embeddable;

@Embeddable
public class ComponentEmptyEmbedded {

private String field1;

private String field2;

public String getField1() {
return field1;
}

public void setField1(String field1) {
this.field1 = field1;
}

public String getField2() {
return field2;
}

public void setField2(String field2) {
this.field2 = field2;
}

}
@@ -0,0 +1,32 @@
package org.hibernate.test.component.empty;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class ComponentEmptyEmbeddedOwner {

@Id
@GeneratedValue
private Integer id;

private ComponentEmptyEmbedded embedded;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public ComponentEmptyEmbedded getEmbedded() {
return embedded;
}

public void setEmbedded(ComponentEmptyEmbedded embedded) {
this.embedded = embedded;
}

}
@@ -0,0 +1,80 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.component.empty;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

/**
* Test class for empty embedded dirtiness computation.
*
* @author Laurent Almeras
*/
public class EmptyCompositesDirtynessTest extends BaseCoreFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { ComponentEmptyEmbeddedOwner.class, ComponentEmptyEmbedded.class };
}

@Override
protected void configure(Configuration configuration) {
super.configure( configuration );
configuration.getProperties().put( Environment.CREATE_EMPTY_COMPOSITES_ENABLED, Boolean.valueOf( false ) );
}

/**
* Test for dirtyness computation consistency when a property is an empty composite.
*/
@Test
@TestForIssue(jiraKey = "HHH-7610")
public void testCompositesEmpty() {
Session s = openSession();
s.getTransaction().begin();

ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner();
s.persist( owner );

s.flush();
s.getTransaction().commit();

s.clear();
s.getTransaction().begin();
owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() );
assertNull( owner.getEmbedded() );
owner.setEmbedded( new ComponentEmptyEmbedded() );

// technically, as all properties are null, update may not be necessary
assertFalse( session.isDirty() ); // must be false to avoid unnecessary updates

s.getTransaction().rollback();
}
}
@@ -0,0 +1,78 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.component.empty;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

/**
* Test class for empty embedded dirtiness computation.
*
* @author Laurent Almeras
*/
public class EmptyInitializedCompositesDirtynessTest extends BaseCoreFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { ComponentEmptyEmbeddedOwner.class, ComponentEmptyEmbedded.class };
}

@Override
protected void configure(Configuration configuration) {
super.configure( configuration );
configuration.getProperties().put( Environment.CREATE_EMPTY_COMPOSITES_ENABLED, Boolean.valueOf( true ) );
}

/**
* Test for dirtyness computation consistency when a property is an empty composite and that empty composite
* initialization is set.
*/
@Test
@TestForIssue(jiraKey = "HHH-7610")
public void testInitializedCompositesEmpty() {
Session s = openSession();
s.getTransaction().begin();

ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner();
s.persist( owner );

s.flush();
s.getTransaction().commit();

s.clear();
s.getTransaction().begin();
owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() );
assertNotNull( owner.getEmbedded() );
assertFalse( s.isDirty() );

s.getTransaction().rollback();
}
}
@@ -0,0 +1,80 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.component.empty;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

/**
* Test class for empty embedded dirtiness computation.
*
* @author Laurent Almeras
*/
public class EmptyInitializedCompositesTest extends BaseCoreFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { ComponentEmptyEmbeddedOwner.class, ComponentEmptyEmbedded.class };
}

@Override
protected void configure(Configuration configuration) {
super.configure( configuration );
configuration.getProperties().put( Environment.CREATE_EMPTY_COMPOSITES_ENABLED, Boolean.valueOf( true ) );
}

/**
* Test empty composite initialization.
*/
@Test
@TestForIssue(jiraKey = "HHH-7610")
public void testCompositesEmpty() {
Session s = openSession();
s.getTransaction().begin();

ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner();
s.persist( owner );

s.flush();
s.getTransaction().commit();

s.clear();
s.getTransaction().begin();
owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() );
assertNotNull( owner.getEmbedded() );
assertFalse( s.isDirty() );

owner.setEmbedded( null );
assertFalse( s.isDirty() ); // must be false to avoid unnecessary updates

s.getTransaction().rollback();
}
}