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.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

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;
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;
}
}
}