diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java b/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java index c3d64ae86c..459b036e46 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java @@ -103,6 +103,9 @@ public class OneToOneSecondPass implements SecondPass { binder.setCascade( cascadeStrategy ); binder.setAccessType( inferredData.getDefaultAccess() ); Property prop = binder.makeProperty(); + if ( ignoreNotFound ) { + prop.setOptional( true ); + } if ( BinderHelper.isEmptyAnnotationValue( mappedBy ) ) { /* * we need to check if the columns are in the right order diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/Coin.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/Coin.java deleted file mode 100644 index 6f72e4de35..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/Coin.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 . - */ - -//$Id$ -package org.hibernate.test.annotations.notfound; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; - -import org.hibernate.annotations.NotFound; -import org.hibernate.annotations.NotFoundAction; - -/** - * @author Emmanuel Bernard - */ -@Entity -public 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; - } - - @ManyToOne - @JoinColumn(name = "currency", referencedColumnName = "name") - @NotFound(action = NotFoundAction.IGNORE) - public Currency getCurrency() { - return currency; - } - - public void setCurrency(Currency currency) { - this.currency = currency; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/Currency.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/Currency.java deleted file mode 100644 index 734f7eb5f4..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/Currency.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 . - */ - -//$Id$ -package org.hibernate.test.annotations.notfound; -import java.io.Serializable; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; - -/** - * @author Emmanuel Bernard - */ -@Entity -public 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; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/NotFoundTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/NotFoundTest.java index 9957692081..b89855635b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/NotFoundTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/notfound/NotFoundTest.java @@ -6,46 +6,199 @@ */ package org.hibernate.test.annotations.notfound; +import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToOne; +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 org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertNull; /** * @author Emmanuel Bernard */ public class NotFoundTest extends BaseCoreFunctionalTestCase { + @Test public void testManyToOne() throws Exception { - Currency euro = new Currency(); + final Currency euro = new Currency(); euro.setName( "Euro" ); - Coin fiveC = new Coin(); - fiveC.setName( "Five cents" ); - fiveC.setCurrency( euro ); - Session s = openSession(); - s.getTransaction().begin(); - s.persist( euro ); - s.persist( fiveC ); - s.getTransaction().commit(); - s.clear(); - Transaction tx = s.beginTransaction(); - euro = (Currency) s.get( Currency.class, euro.getId() ); - s.delete( euro ); - tx.commit(); - s.clear(); - tx = s.beginTransaction(); - fiveC = (Coin) s.get( Coin.class, fiveC.getId() ); - assertNull( fiveC.getCurrency() ); - s.delete( fiveC ); - tx.commit(); - s.close(); + + final Coin fiveCents = new Coin(); + fiveCents.setName( "Five cents" ); + fiveCents.setCurrency( euro ); + + doInHibernate( this::sessionFactory, session -> { + session.persist( euro ); + session.persist( fiveCents ); + } ); + + doInHibernate( this::sessionFactory, session -> { + Currency _euro = session.get( Currency.class, euro.getId() ); + session.delete( _euro ); + } ); + + doInHibernate( this::sessionFactory, session -> { + Coin _fiveCents = session.get( Coin.class, fiveCents.getId() ); + assertNull( _fiveCents.getCurrency() ); + session.delete( _fiveCents ); + } ); + } + + @Test + public void testOneToOne() throws Exception { + doInHibernate( this::sessionFactory, session -> { + Show show = new Show(); + session.save( show ); + + ShowDescription showDescription = new ShowDescription(); + session.save( showDescription ); + } ); } @Override protected Class[] getAnnotatedClasses() { - return new Class[] { Coin.class, Currency.class }; + return new Class[] { + Coin.class, + Currency.class, + Show.class, + ShowDescription.class + }; + } + + @Entity(name = "Coin") + 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; + } + + @ManyToOne + @JoinColumn(name = "currency", referencedColumnName = "name") + @NotFound(action = NotFoundAction.IGNORE) + public Currency getCurrency() { + return currency; + } + + public void setCurrency(Currency currency) { + this.currency = currency; + } + } + + @Entity(name = "Currency") + 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; + } + } + + @Entity(name = "Show") + public static class Show { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @OneToOne() + @NotFound(action = NotFoundAction.IGNORE) + @JoinTable(name = "Show_Description", + joinColumns = @JoinColumn(name = "show_id"), + inverseJoinColumns = @JoinColumn(name = "description_id")) + private ShowDescription description; + + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public ShowDescription getDescription() { + return description; + } + + public void setDescription(ShowDescription description) { + this.description = description; + } + } + + @Entity(name = "ShowDescription") + public static class ShowDescription { + + @Id + @GeneratedValue + private Integer id; + + @NotFound(action = NotFoundAction.IGNORE) + @OneToOne(mappedBy = "description") + private Show show; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Show getShow() { + return show; + } + + public void setShow(Show show) { + this.show = show; + } } }