HHH-11305 - @OneToOne association, Nullable check does is not skipped for @NotFound(action = NotFoundAction.IGNORE)

This commit is contained in:
Andrea Boriero 2016-12-05 21:20:45 +00:00 committed by Vlad Mihalcea
parent e720b2872a
commit fc7f0fca73
4 changed files with 182 additions and 122 deletions

View File

@ -103,6 +103,9 @@ public class OneToOneSecondPass implements SecondPass {
binder.setCascade( cascadeStrategy ); binder.setCascade( cascadeStrategy );
binder.setAccessType( inferredData.getDefaultAccess() ); binder.setAccessType( inferredData.getDefaultAccess() );
Property prop = binder.makeProperty(); Property prop = binder.makeProperty();
if ( ignoreNotFound ) {
prop.setOptional( true );
}
if ( BinderHelper.isEmptyAnnotationValue( mappedBy ) ) { if ( BinderHelper.isEmptyAnnotationValue( mappedBy ) ) {
/* /*
* we need to check if the columns are in the right order * we need to check if the columns are in the right order

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
//$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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
//$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;
}
}

View File

@ -6,46 +6,199 @@
*/ */
package org.hibernate.test.annotations.notfound; 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.junit.Test;
import org.hibernate.Session; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */
public class NotFoundTest extends BaseCoreFunctionalTestCase { public class NotFoundTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testManyToOne() throws Exception { public void testManyToOne() throws Exception {
Currency euro = new Currency(); final Currency euro = new Currency();
euro.setName( "Euro" ); euro.setName( "Euro" );
Coin fiveC = new Coin();
fiveC.setName( "Five cents" ); final Coin fiveCents = new Coin();
fiveC.setCurrency( euro ); fiveCents.setName( "Five cents" );
Session s = openSession(); fiveCents.setCurrency( euro );
s.getTransaction().begin();
s.persist( euro ); doInHibernate( this::sessionFactory, session -> {
s.persist( fiveC ); session.persist( euro );
s.getTransaction().commit(); session.persist( fiveCents );
s.clear(); } );
Transaction tx = s.beginTransaction();
euro = (Currency) s.get( Currency.class, euro.getId() ); doInHibernate( this::sessionFactory, session -> {
s.delete( euro ); Currency _euro = session.get( Currency.class, euro.getId() );
tx.commit(); session.delete( _euro );
s.clear(); } );
tx = s.beginTransaction();
fiveC = (Coin) s.get( Coin.class, fiveC.getId() ); doInHibernate( this::sessionFactory, session -> {
assertNull( fiveC.getCurrency() ); Coin _fiveCents = session.get( Coin.class, fiveCents.getId() );
s.delete( fiveC ); assertNull( _fiveCents.getCurrency() );
tx.commit(); session.delete( _fiveCents );
s.close(); } );
}
@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 @Override
protected Class[] getAnnotatedClasses() { 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;
}
} }
} }