HHH-12226 : ObjectNotFoundException thrown when @NotFound(action = NotFoundAction.IGNORE) used with enhancement

(cherry picked from commit 9fe24e7e6d)
This commit is contained in:
Gail Badner 2018-03-23 00:38:00 -07:00
parent 2903551b19
commit 76e3bead49
6 changed files with 615 additions and 1 deletions

View File

@ -681,7 +681,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
getAssociatedEntityName(),
id,
eager,
isNullable() && !isProxyUnwrapEnabled
isNullable()
);
if ( proxyOrEntity instanceof HibernateProxy ) {

View File

@ -0,0 +1,129 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.annotations.notfound;
import java.io.Serializable;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertNull;
/**
* @author Emmanuel Bernard
* @author Gail Badner
*/
public class NotFoundLogicalOneToOneTest extends BaseCoreFunctionalTestCase {
@Test
public void testLogicalOneToOne() throws Exception {
Currency euro = new Currency();
euro.setName( "Euro" );
Coin fiveC = new Coin();
fiveC.setName( "Five cents" );
fiveC.setCurrency( euro );
doInHibernate(
this::sessionFactory, session -> {
session.persist( euro );
session.persist( fiveC );
}
);
doInHibernate(
this::sessionFactory, session -> {
session.delete( euro );
}
);
doInHibernate(
this::sessionFactory, session -> {
Coin coin = session.get( Coin.class, fiveC.getId() );
assertNull( coin.getCurrency() );
session.delete( coin );
}
);
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Coin.class, Currency.class };
}
@Entity
public static class Coin {
private Integer id;
private String name;
private Currency currency;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
@NotFound(action = NotFoundAction.IGNORE)
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
}
@Entity
public static class Currency implements Serializable {
private Integer id;
private String name;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@ -0,0 +1,91 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.annotations.notfound;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Gail Badner
*/
public class NotFoundOneToOneNonInsertableNonUpdateableTest extends BaseCoreFunctionalTestCase {
private static final int ID = 1;
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
Person.class,
PersonInfo.class
};
}
@Test
public void testOneToOne() {
doInHibernate(
this::sessionFactory, session -> {
Person person = new Person();
person.id = ID;
person.personInfo = new PersonInfo();
person.personInfo.id = ID;
session.persist( person );
}
);
doInHibernate(
this::sessionFactory, session -> {
session.delete( session.get( PersonInfo.class, ID ) );
}
);
doInHibernate(
this::sessionFactory, session -> {
Person person = session.get( Person.class, ID );
assertNotNull( person );
assertNull( person.personInfo );
session.delete( person );
}
);
}
@Entity(name="Person")
public static class Person {
@Id
private int id;
@OneToOne(optional = true, cascade = CascadeType.ALL)
@JoinColumn(
name = "id",
updatable = false,
insertable = false
)
@NotFound(action = NotFoundAction.IGNORE)
private PersonInfo personInfo;
}
@Entity(name = "PersonInfo")
public static class PersonInfo {
@Id
private int id;
}
}

View File

@ -0,0 +1,132 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.bytecode.enhancement.lazy.notfound;
/**
* @author Gail Badner
*/
import javax.persistence.CascadeType;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@TestForIssue( jiraKey = "HHH-12226")
@RunWith( BytecodeEnhancerRunner.class )
public class LazyNotFoundManyToOneNonUpdatableNonInsertableTest extends BaseCoreFunctionalTestCase {
private static int ID = 1;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
User.class,
Lazy.class
};
}
@Test
public void test() {
doInHibernate(
this::sessionFactory, session -> {
Lazy p = new Lazy();
p.id = ID;
User u = new User();
u.id = ID;
u.setLazy( p );
session.persist( u );
}
);
doInHibernate(
this::sessionFactory, session -> {
session.delete( session.get( Lazy.class, ID ) );
}
);
doInHibernate(
this::sessionFactory, session -> {
User user = session.find( User.class, ID );
assertFalse( Hibernate.isPropertyInitialized( user, "lazy" ) );
assertNull( user.getLazy() );
}
);
}
@Entity(name="User")
@Table(name = "USER_TABLE")
public static class User {
@Id
private Integer id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = true)
@LazyToOne(value = LazyToOneOption.NO_PROXY)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT),
name = "id",
referencedColumnName = "id",
insertable = false,
updatable = false
)
private Lazy lazy;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Lazy getLazy() {
return lazy;
}
public void setLazy(Lazy lazy) {
this.lazy = lazy;
}
}
@Entity(name = "Lazy")
@Table(name = "LAZY")
public static class Lazy {
@Id
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}

View File

@ -0,0 +1,134 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.bytecode.enhancement.lazy.notfound;
/**
* @author Gail Badner
*/
import javax.persistence.CascadeType;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@TestForIssue( jiraKey = "HHH-12226")
@RunWith( BytecodeEnhancerRunner.class )
public class LazyNotFoundOneToOneNonUpdatableNonInsertableTest extends BaseCoreFunctionalTestCase {
private static int ID = 1;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
User.class,
Lazy.class
};
}
@Test
public void test() {
doInHibernate(
this::sessionFactory, session -> {
Lazy p = new Lazy();
p.id = ID;
User u = new User();
u.id = ID;
u.setLazy( p );
session.persist( u );
}
);
doInHibernate(
this::sessionFactory, session -> {
session.delete( session.get( Lazy.class, ID ) );
}
);
doInHibernate(
this::sessionFactory, session -> {
User user = session.find( User.class, ID );
assertFalse( Hibernate.isPropertyInitialized( user, "lazy" ) );
assertNull( user.getLazy() );
assertTrue( Hibernate.isPropertyInitialized( user, "lazy" ) );
}
);
}
@Entity(name="User")
@Table(name = "USER_TABLE")
public static class User {
@Id
private Integer id;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = true)
@LazyToOne(value = LazyToOneOption.NO_PROXY)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT),
name = "id",
referencedColumnName = "id",
insertable = false,
updatable = false
)
private Lazy lazy;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Lazy getLazy() {
return lazy;
}
public void setLazy(Lazy lazy) {
this.lazy = lazy;
}
}
@Entity(name = "Lazy")
@Table(name = "LAZY")
public static class Lazy {
@Id
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}

View File

@ -0,0 +1,128 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.bytecode.enhancement.lazy.notfound;
/**
* @author Gail Badner
*/
import javax.persistence.CascadeType;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@TestForIssue( jiraKey = "HHH-12226")
@RunWith( BytecodeEnhancerRunner.class )
public class LazyNotFoundOneToOneTest extends BaseCoreFunctionalTestCase {
private static int ID = 1;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
User.class,
Lazy.class
};
}
@Test
public void test() {
doInHibernate(
this::sessionFactory, session -> {
Lazy p = new Lazy();
p.id = ID;
User u = new User();
u.id = ID;
u.setLazy( p );
session.persist( u );
}
);
doInHibernate(
this::sessionFactory, session -> {
session.delete( session.get( Lazy.class, ID ) );
}
);
doInHibernate(
this::sessionFactory, session -> {
User user = session.find( User.class, ID );
assertFalse( Hibernate.isPropertyInitialized( user, "lazy" ) );
assertNull( user.getLazy() );
}
);
}
@Entity(name="User")
@Table(name = "USER_TABLE")
public static class User {
@Id
private Integer id;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@LazyToOne(value = LazyToOneOption.NO_PROXY)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Lazy lazy;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Lazy getLazy() {
return lazy;
}
public void setLazy(Lazy lazy) {
this.lazy = lazy;
}
}
@Entity(name = "Lazy")
@Table(name = "LAZY")
public static class Lazy {
@Id
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}