Skip to content

Commit

Permalink
HHH-11305 - @OnetoOne association, Nullable check does is not skipped…
Browse files Browse the repository at this point in the history
… for @NotFound(action = NotFoundAction.IGNORE)
  • Loading branch information
dreab8 authored and vladmihalcea committed Dec 7, 2016
1 parent e720b28 commit fc7f0fc
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public void doSecondPass(Map persistentClasses) throws MappingException {
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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,199 @@
*/
package org.hibernate.test.annotations.notfound;

import org.junit.Test;
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.Session;
import org.hibernate.Transaction;
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
*/
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;
}
}
}

0 comments on commit fc7f0fc

Please sign in to comment.