Author: epbernard
Date: 2008-03-03 21:07:51 -0500 (Mon, 03 Mar 2008)
New Revision: 14384
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/BasketItems.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/BasketItemsPK.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/Customers.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOneTest.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/ShoppingBaskets.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/ShoppingBasketsPK.java
Modified:
annotations/trunk/src/java/org/hibernate/cfg/ToOneFkSecondPass.java
Log:
ANN-590 Alphabetical order issue when @ManyToOne is used in PrimaryKey and explicit
referencedColumnName are used
Modified: annotations/trunk/src/java/org/hibernate/cfg/ToOneFkSecondPass.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/ToOneFkSecondPass.java 2008-03-04
00:22:13 UTC (rev 14383)
+++ annotations/trunk/src/java/org/hibernate/cfg/ToOneFkSecondPass.java 2008-03-04
02:07:51 UTC (rev 14384)
@@ -1,6 +1,8 @@
// $Id$
package org.hibernate.cfg;
+import java.util.Iterator;
+
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
@@ -10,6 +12,8 @@
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.ToOne;
+import org.hibernate.mapping.KeyValue;
+import org.hibernate.mapping.Component;
import org.hibernate.util.StringHelper;
/**
@@ -41,8 +45,32 @@
public boolean isInPrimaryKey() {
if ( entityClassName == null ) return false;
- Property property = mappings.getClass( entityClassName ).getIdentifierProperty();
- return property != null && path != null && path.startsWith(
property.getName() );
+ final PersistentClass persistentClass = mappings.getClass( entityClassName );
+ Property property = persistentClass.getIdentifierProperty();
+ if ( path == null ) {
+ return false;
+ }
+ else if ( property != null) {
+ //try explicit identifier property
+ return path.startsWith( property.getName() );
+ }
+ else {
+ //try the embedded property
+ //embedded property starts their path with 'id.' See PropertyPreloadedData( )
use when idClass != null in AnnotationBinder
+ if ( path.startsWith( "id." ) ) {
+ KeyValue valueIdentifier = persistentClass.getIdentifier();
+ String localPath = path.substring( 3 );
+ if ( valueIdentifier instanceof Component ) {
+ Iterator it = ( (Component) valueIdentifier ).getPropertyIterator();
+ while ( it.hasNext() ) {
+ Property idProperty = (Property) it.next();
+ if ( localPath.startsWith( idProperty.getName() ) ) return true;
+ }
+
+ }
+ }
+ }
+ return false;
}
public void doSecondPass(java.util.Map persistentClasses) throws MappingException {
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/BasketItems.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/BasketItems.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/BasketItems.java 2008-03-04
02:07:51 UTC (rev 14384)
@@ -0,0 +1,56 @@
+//$
+package org.hibernate.test.annotations.idmanytoone;
+
+import java.io.Serializable;
+import javax.persistence.IdClass;
+import javax.persistence.Id;
+import javax.persistence.Column;
+import javax.persistence.CascadeType;
+import javax.persistence.FetchType;
+import javax.persistence.JoinColumn;
+import javax.persistence.Table;
+import javax.persistence.ManyToOne;
+import javax.persistence.Basic;
+import javax.persistence.JoinColumns;
+import javax.persistence.Entity;
+
+@Entity
+@Table(name="BasketItems")
+(a)org.hibernate.annotations.Proxy(lazy=false)
+(a)IdClass(BasketItemsPK.class)
+public class BasketItems implements Serializable {
+
+ private static final long serialVersionUID = -4580497316918713849L;
+
+ @Id
+ @ManyToOne(cascade={ CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
+ @JoinColumns({ @JoinColumn(name="basketDatetime",
referencedColumnName="basketDatetime"), @JoinColumn(name="customerID",
referencedColumnName="customerID") })
+ @Basic(fetch= FetchType.LAZY)
+ private ShoppingBaskets shoppingBaskets;
+
+ @Column(name="cost", nullable=false)
+ @Id
+ private Double cost;
+
+ public void setCost(double value) {
+ setCost(new Double(value));
+ }
+
+ public void setCost(Double value) {
+ this.cost = value;
+ }
+
+ public Double getCost() {
+ return cost;
+ }
+
+ public void setShoppingBaskets(ShoppingBaskets value) {
+ this.shoppingBaskets = value;
+ }
+
+ public ShoppingBaskets getShoppingBaskets() {
+ return shoppingBaskets;
+ }
+
+}
+
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/BasketItemsPK.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/BasketItemsPK.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/BasketItemsPK.java 2008-03-04
02:07:51 UTC (rev 14384)
@@ -0,0 +1,72 @@
+//$
+package org.hibernate.test.annotations.idmanytoone;
+
+import java.io.Serializable;
+import javax.persistence.CascadeType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.Column;
+import javax.persistence.ManyToOne;
+import javax.persistence.Basic;
+import javax.persistence.Embeddable;
+import javax.persistence.FetchType;
+
+@Embeddable
+public class BasketItemsPK implements Serializable {
+
+ private static final long serialVersionUID = 3585214409096105241L;
+
+ public boolean equals(Object aObj) {
+ if (aObj == this)
+ return true;
+ if (!(aObj instanceof BasketItemsPK))
+ return false;
+ BasketItemsPK basketitemspk = (BasketItemsPK)aObj;
+ if (getShoppingBaskets() == null && basketitemspk.getShoppingBaskets() !=
null)
+ return false;
+ if (!getShoppingBaskets().equals(basketitemspk.getShoppingBaskets()))
+ return false;
+ if ((getCost() != null && !getCost().equals(basketitemspk.getCost())) ||
(getCost() == null && basketitemspk.getCost() != null))
+ return false;
+ return true;
+ }
+
+ public int hashCode() {
+ int hashcode = 0;
+ if (getShoppingBaskets() != null) {
+ hashcode = hashcode + (getShoppingBaskets().getOwner() == null ? 0 :
getShoppingBaskets().getOwner().hashCode());
+ hashcode = hashcode + (getShoppingBaskets().getBasketDatetime() == null ? 0 :
getShoppingBaskets().getBasketDatetime().hashCode());
+ }
+ hashcode = hashcode + (getCost() == null ? 0 : getCost().hashCode());
+ return hashcode;
+ }
+
+ @Id
+ @ManyToOne(cascade={ CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
+ @JoinColumns({ @JoinColumn(name="basketDatetime",
referencedColumnName="basketDatetime"), @JoinColumn(name="customerID",
referencedColumnName="customerID") })
+ @Basic(fetch= FetchType.LAZY)
+ private ShoppingBaskets shoppingBaskets;
+
+ public void setShoppingBaskets(ShoppingBaskets value) {
+ this.shoppingBaskets = value;
+ }
+
+ public ShoppingBaskets getShoppingBaskets() {
+ return this.shoppingBaskets;
+ }
+
+ @Column(name="cost", nullable=false)
+ @Id
+ private Double cost;
+
+ public void setCost(Double value) {
+ this.cost = value;
+ }
+
+ public Double getCost() {
+ return this.cost;
+ }
+
+}
+
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/Customers.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/Customers.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/Customers.java 2008-03-04
02:07:51 UTC (rev 14384)
@@ -0,0 +1,48 @@
+//$
+package org.hibernate.test.annotations.idmanytoone;
+
+import java.io.Serializable;
+import javax.persistence.Id;
+import javax.persistence.Column;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="Customers")
+(a)org.hibernate.annotations.Proxy(lazy=false)
+public class Customers implements Serializable {
+
+ private static final long serialVersionUID = -885167444315163039L;
+
+ @Column(name="customerID", nullable=false)
+ @Id
+ private int customerID;
+
+ @OneToMany(mappedBy="owner", cascade= CascadeType.ALL,
targetEntity=ShoppingBaskets.class)
+ @org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
+ private java.util.Set shoppingBasketses = new java.util.HashSet();
+
+ public void setCustomerID(int value) {
+ this.customerID = value;
+ }
+
+ public int getCustomerID() {
+ return customerID;
+ }
+
+ public int getORMID() {
+ return getCustomerID();
+ }
+
+ public void setShoppingBasketses(java.util.Set value) {
+ this.shoppingBasketses = value;
+ }
+
+ public java.util.Set getShoppingBasketses() {
+ return shoppingBasketses;
+ }
+
+}
+
Copied:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOneTest.java
(from rev 14382,
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOne.java)
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOneTest.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOneTest.java 2008-03-04
02:07:51 UTC (rev 14384)
@@ -0,0 +1,82 @@
+//$Id: $
+package org.hibernate.test.annotations.idmanytoone;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class IdManyToOneTest extends TestCase {
+ public void testFkCreationOrdering() throws Exception {
+ //no real test case, the sessionFactory building is tested
+ Session s = openSession();
+ s.close();
+ }
+
+ public void getBiDirOneToManyInId() throws Exception {
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ CardKey key = new CardKey();
+ s.persist( key );
+ Project project = new Project();
+ s.persist( project );
+ Card card = new Card();
+ card.getPrimaryKey().setProject( project );
+ s.persist( card );
+ CardField field = new CardField();
+ field.getPrimaryKey().setKey( key );
+ field.getPrimaryKey().setCard( card );
+ s.persist( field );
+ card.setMainCardField( field );
+ s.flush();
+ s.clear();
+ card = (Card) s.createQuery( "from Card c").list().get(0);
+ assertEquals( 1, card.getFields().size() );
+ assertEquals( card.getMainCardField(), card.getFields().iterator().next() );
+ tx.rollback();
+ s.close();
+ }
+
+ public void testIdClassManyToOne() {
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ Store store = new Store();
+ Customer customer = new Customer();
+ s.persist( store );
+ s.persist( customer );
+ StoreCustomer sc = new StoreCustomer( store, customer );
+ s.persist( sc );
+ s.flush();
+ s.clear();
+
+ store = (Store) s.get(Store.class, store.id );
+ assertEquals( 1, store.customers.size() );
+ assertEquals( customer.id, store.customers.iterator().next().customer.id );
+ tx.rollback();
+
+ //TODO test Customers / ShoppingBaskets / BasketItems
testIdClassManyToOneWithReferenceColumn
+ s.close();
+ }
+
+ protected Class[] getMappings() {
+ return new Class[] {
+ Store.class,
+ Customer.class,
+ StoreCustomer.class,
+ CardKey.class,
+ CardField.class,
+ Card.class,
+ Project.class,
+
+ //tested only through deployment
+ //ANN-590 testIdClassManyToOneWithReferenceColumn
+ Customers.class,
+ ShoppingBaskets.class,
+ ShoppingBasketsPK.class,
+ BasketItems.class,
+ BasketItemsPK.class
+ };
+ }
+}
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/ShoppingBaskets.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/ShoppingBaskets.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/ShoppingBaskets.java 2008-03-04
02:07:51 UTC (rev 14384)
@@ -0,0 +1,66 @@
+//$
+package org.hibernate.test.annotations.idmanytoone;
+
+import java.io.Serializable;
+
+import javax.persistence.Basic;
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Id;
+import javax.persistence.IdClass;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.ManyToOne;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="ShoppingBasket")
+(a)org.hibernate.annotations.Proxy(lazy=false)
+(a)IdClass(ShoppingBasketsPK.class)
+public class ShoppingBaskets implements Serializable {
+
+ private static final long serialVersionUID = 4739240471638885734L;
+
+ @Id
+ @ManyToOne(cascade={ CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
+ @JoinColumns({ @JoinColumn(name="customerID",
referencedColumnName="customerID") })
+ @Basic(fetch=FetchType.LAZY)
+ private Customers owner;
+
+ @Column(name="basketDatetime", nullable=false)
+ @Id
+ private java.util.Date basketDatetime;
+
+ @OneToMany(mappedBy="shoppingBaskets", cascade=CascadeType.ALL,
targetEntity=BasketItems.class)
+ @org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
+ private java.util.Set items = new java.util.HashSet();
+
+ public void setBasketDatetime(java.util.Date value) {
+ this.basketDatetime = value;
+ }
+
+ public java.util.Date getBasketDatetime() {
+ return basketDatetime;
+ }
+
+ public void setOwner(Customers value) {
+ this.owner = value;
+ }
+
+ public Customers getOwner() {
+ return owner;
+ }
+
+ public void setItems(java.util.Set value) {
+ this.items = value;
+ }
+
+ public java.util.Set getItems() {
+ return items;
+ }
+
+}
+
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/ShoppingBasketsPK.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/ShoppingBasketsPK.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/ShoppingBasketsPK.java 2008-03-04
02:07:51 UTC (rev 14384)
@@ -0,0 +1,70 @@
+//$
+package org.hibernate.test.annotations.idmanytoone;
+
+import java.io.Serializable;
+import javax.persistence.CascadeType;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.JoinColumns;
+import javax.persistence.FetchType;
+import javax.persistence.Basic;
+import javax.persistence.JoinColumn;
+import javax.persistence.Column;
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class ShoppingBasketsPK implements Serializable {
+ private static final long serialVersionUID = 4121297376338222776L;
+
+ public boolean equals(Object aObj) {
+ if (aObj == this)
+ return true;
+ if (!(aObj instanceof ShoppingBasketsPK))
+ return false;
+ ShoppingBasketsPK shoppingbasketspk = (ShoppingBasketsPK)aObj;
+ if (getOwner() == null && shoppingbasketspk.getOwner() != null)
+ return false;
+ if (!getOwner().equals(shoppingbasketspk.getOwner()))
+ return false;
+ if (getBasketDatetime() != shoppingbasketspk.getBasketDatetime())
+ return false;
+ return true;
+ }
+
+ public int hashCode() {
+ int hashcode = 0;
+ if (getOwner() != null) {
+ hashcode = hashcode + (int) getOwner().getORMID();
+ }
+ hashcode = hashcode + (getBasketDatetime() == null ? 0 :
getBasketDatetime().hashCode());
+ return hashcode;
+ }
+
+ @Id
+ @ManyToOne(cascade={ CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
+ @JoinColumns({ @JoinColumn(name="customerID",
referencedColumnName="customerID") })
+ @Basic(fetch= FetchType.LAZY)
+ private Customers owner;
+
+ public void setOwner(Customers value) {
+ this.owner = value;
+ }
+
+ public Customers getOwner() {
+ return this.owner;
+ }
+
+ @Column(name="basketDatetime", nullable=false)
+ @Id
+ private java.util.Date basketDatetime;
+
+ public void setBasketDatetime(java.util.Date value) {
+ this.basketDatetime = value;
+ }
+
+ public java.util.Date getBasketDatetime() {
+ return this.basketDatetime;
+ }
+
+}
+