HHH-5977 Add a couple additional unit tests for when you use a @JoinColumn with a secondary table specified.

This commit is contained in:
David M. Carr 2011-02-24 17:14:08 -05:00 committed by Emmanuel Bernard
parent f7fda73168
commit 79e0909772
4 changed files with 71 additions and 0 deletions

View File

@ -269,6 +269,26 @@ public class ManyToOneTest extends TestCase {
s.close();
}
public void testManyToOneNonPkSecondaryTable() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Order order = new Order();
order.setOrderNbr( "123" );
s.persist( order );
OrderLine ol = new OrderLine();
ol.setItem( "Mouse" );
ol.setReplacementOrder( order );
s.persist( ol );
s.flush();
s.clear();
ol = (OrderLine) s.get( OrderLine.class, ol.getId() );
assertNotNull( ol.getReplacementOrder() );
assertEquals( "123", ol.getReplacementOrder().getOrderNbr() );
assertFalse( ol.getReplacementOrder().getOrderLines().contains( ol ) );
tx.rollback();
s.close();
}
public void testTwoManyToOneNonPk() throws Exception {
//2 many to one non pk pointing to the same referencedColumnName should not fail
Session s = openSession();

View File

@ -6,15 +6,18 @@ import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;
import javax.persistence.SecondaryTable;
/**
* @author Emmanuel Bernard
*/
@Entity
@SecondaryTable(name="OrderLine_Extension")
public class OrderLine {
private Integer id;
private String item;
private Order order;
private Order replacementOrder;
@Id @GeneratedValue
public Integer getId() {
@ -42,4 +45,14 @@ public class OrderLine {
public void setOrder(Order order) {
this.order = order;
}
@ManyToOne
@JoinColumn(name="replacement_order_nbr", table="OrderLine_Extension", referencedColumnName = "order_nbr")
public Order getReplacementOrder() {
return replacementOrder;
}
public void setReplacementOrder(Order replacementOrder) {
this.replacementOrder = replacementOrder;
}
}

View File

@ -7,16 +7,19 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SecondaryTable;
/**
* @author Emmanuel Bernard
*/
@Entity
@SecondaryTable(name="CLIENT_EXTENSION")
public class Client {
private Integer id;
private String name;
private Address address;
private Address secondaryAddress;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "ADDRESS_ID")
@ -28,6 +31,16 @@ public class Client {
this.address = address;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "SECONDARY_ADDRESS_ID", table="CLIENT_EXTENSION")
public Address getSecondaryAddress() {
return secondaryAddress;
}
public void setSecondaryAddress(Address secondaryAddress) {
this.secondaryAddress = secondaryAddress;
}
@Id
@GeneratedValue
public Integer getId() {

View File

@ -108,6 +108,31 @@ public class OneToOneTest extends TestCase {
s.close();
}
public void testOneToOneWithExplicitSecondaryTableFk() throws Exception {
Client c = new Client();
Address a = new Address();
a.setCity( "Paris" );
c.setName( "Emmanuel" );
c.setSecondaryAddress( a );
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist( c );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
c = ( Client ) s.get( Client.class, c.getId() );
assertNotNull( c );
assertNotNull( c.getSecondaryAddress() );
assertEquals( "Paris", c.getSecondaryAddress().getCity() );
tx.commit();
s.close();
}
public void testUnidirectionalTrueOneToOne() throws Exception {
Body b = new Body();
Heart h = new Heart();