Hibernate SVN: r14386 - in annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone: alphabetical and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2008-03-03 22:52:43 -0500 (Mon, 03 Mar 2008)
New Revision: 14386
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/A.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/AlphabeticalIdManyToOneTest.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/B.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/BId.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/C.java
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/CId.java
Log:
ANN-676 tests
Added: annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/A.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/A.java (rev 0)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/A.java 2008-03-04 03:52:43 UTC (rev 14386)
@@ -0,0 +1,39 @@
+//$
+package org.hibernate.test.annotations.idmanytoone.alphabetical;
+
+import java.util.List;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+@Entity
+public class A {
+
+ @Id
+ private int id;
+
+ @OneToMany( mappedBy = "parent" )
+ List<C> children;
+
+ public A() {
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public List<C> getChildren() {
+ return children;
+ }
+
+ public void setChildren(List<C> children) {
+ this.children = children;
+ }
+
+
+}
+
Added: annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/AlphabeticalIdManyToOneTest.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/AlphabeticalIdManyToOneTest.java (rev 0)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/AlphabeticalIdManyToOneTest.java 2008-03-04 03:52:43 UTC (rev 14386)
@@ -0,0 +1,24 @@
+//$
+package org.hibernate.test.annotations.idmanytoone.alphabetical;
+
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class AlphabeticalIdManyToOneTest extends TestCase {
+ public void testAlphabeticalTest() throws Exception {
+ //test through deployment
+ }
+
+
+ protected Class[] getMappings() {
+ return new Class[] {
+ B.class,
+ C.class,
+ A.class
+
+
+ };
+ }
+}
Added: annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/B.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/B.java (rev 0)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/B.java 2008-03-04 03:52:43 UTC (rev 14386)
@@ -0,0 +1,39 @@
+//$
+package org.hibernate.test.annotations.idmanytoone.alphabetical;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.IdClass;
+
+@Entity
+@IdClass( BId.class )
+public class B {
+
+ @Id
+ private C parent;
+
+ @Id
+ private int sequenceNumber;
+
+ public B() {
+ }
+
+ public C getParent() {
+ return parent;
+ }
+
+ public void setParent(C parent) {
+ this.parent = parent;
+ }
+
+ public int getSequenceNumber() {
+ return sequenceNumber;
+ }
+
+ public void setSequenceNumber(int sequenceNumber) {
+ this.sequenceNumber = sequenceNumber;
+ }
+
+
+}
+
Added: annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/BId.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/BId.java (rev 0)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/BId.java 2008-03-04 03:52:43 UTC (rev 14386)
@@ -0,0 +1,69 @@
+//$
+package org.hibernate.test.annotations.idmanytoone.alphabetical;
+
+import java.io.Serializable;
+import javax.persistence.Embeddable;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.ManyToOne;
+
+@Embeddable
+public class BId implements Serializable {
+
+ @ManyToOne
+ @JoinColumns( {
+ @JoinColumn( name = "aId", nullable = false ),
+ @JoinColumn( name = "bSequenceNumber", nullable = false )
+ } )
+ private C parent;
+
+ private int sequenceNumber;
+
+ public BId() {
+ }
+
+ public C getParent() {
+ return parent;
+ }
+
+ public void setParent(C parent) {
+ this.parent = parent;
+ }
+
+ public int getSequenceNumber() {
+ return sequenceNumber;
+ }
+
+ public void setSequenceNumber(int sequenceNumber) {
+ this.sequenceNumber = sequenceNumber;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ( ( parent == null ) ? 0 : parent.hashCode() );
+ result = prime * result + sequenceNumber;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if ( this == obj )
+ return true;
+ if ( obj == null )
+ return false;
+ if ( getClass() != obj.getClass() )
+ return false;
+ final BId other = (BId) obj;
+ if ( parent == null ) {
+ if ( other.parent != null )
+ return false;
+ }
+ else if ( !parent.equals( other.parent ) )
+ return false;
+ if ( sequenceNumber != other.sequenceNumber )
+ return false;
+ return true;
+ }
+}
Added: annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/C.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/C.java (rev 0)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/C.java 2008-03-04 03:52:43 UTC (rev 14386)
@@ -0,0 +1,52 @@
+//$
+package org.hibernate.test.annotations.idmanytoone.alphabetical;
+
+import java.util.List;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.IdClass;
+import javax.persistence.OneToMany;
+
+@Entity
+@IdClass( CId.class )
+public class C {
+
+ @Id
+ private A parent;
+
+ @Id
+ private int sequenceNumber;
+
+ @OneToMany( mappedBy = "parent" )
+ List<B> children;
+
+ public C() {
+ }
+
+ public A getParent() {
+ return parent;
+ }
+
+ public void setParent(A parent) {
+ this.parent = parent;
+ }
+
+ public int getSequenceNumber() {
+ return sequenceNumber;
+ }
+
+ public void setSequenceNumber(int sequenceNumber) {
+ this.sequenceNumber = sequenceNumber;
+ }
+
+ public List<B> getChildren() {
+ return children;
+ }
+
+ public void setChildren(List<B> children) {
+ this.children = children;
+ }
+
+
+}
+
Added: annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/CId.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/CId.java (rev 0)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/alphabetical/CId.java 2008-03-04 03:52:43 UTC (rev 14386)
@@ -0,0 +1,67 @@
+//$
+package org.hibernate.test.annotations.idmanytoone.alphabetical;
+
+import java.io.Serializable;
+import javax.persistence.Embeddable;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+
+@Embeddable
+public class CId implements Serializable {
+
+ @ManyToOne
+ @JoinColumn( name = "aId", nullable = false )
+ private A parent;
+
+ private int sequenceNumber;
+
+ public CId() {
+ }
+
+ public A getParent() {
+ return parent;
+ }
+
+ public void setParent(A parent) {
+ this.parent = parent;
+ }
+
+ public int getSequenceNumber() {
+ return sequenceNumber;
+ }
+
+ public void setSequenceNumber(int sequenceNumber) {
+ this.sequenceNumber = sequenceNumber;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ( ( parent == null ) ? 0 : parent.hashCode() );
+ result = prime * result + sequenceNumber;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if ( this == obj )
+ return true;
+ if ( obj == null )
+ return false;
+ if ( getClass() != obj.getClass() )
+ return false;
+ final CId other = (CId) obj;
+ if ( parent == null ) {
+ if ( other.parent != null )
+ return false;
+ }
+ else if ( !parent.equals( other.parent ) )
+ return false;
+ if ( sequenceNumber != other.sequenceNumber )
+ return false;
+ return true;
+ }
+
+
+}
16 years, 8 months
Hibernate SVN: r14385 - in annotations/trunk/src: test/org/hibernate/test/annotations/generics and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2008-03-03 22:37:29 -0500 (Mon, 03 Mar 2008)
New Revision: 14385
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/generics/Dummy.java
Removed:
annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOne.java
Modified:
annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java
annotations/trunk/src/test/org/hibernate/test/annotations/generics/GenericsTest.java
Log:
ANN-698 ignore unbound property if default field access is used.
Modified: annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java 2008-03-04 02:07:51 UTC (rev 14384)
+++ annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java 2008-03-04 03:37:29 UTC (rev 14385)
@@ -848,19 +848,32 @@
String accessType = explicitAccessType != null ?
explicitAccessType :
"property";
+ /*
+ * delay the exception in case field access is used
+ */
+ AnnotationException exceptionWhileWalkingElements = null;
+ try {
+ for (int index = 0; index < deep; index++) {
+ XClass clazz = classesToProcess.get( index );
- for (int index = 0; index < deep; index++) {
- XClass clazz = classesToProcess.get( index );
-
- boolean currentHasIdentifier = addElementsOfAClass(
- elements, propertyHolder, isPropertyAnnotated,
- accessType, clazz, mappings
- );
- hasIdentifier = hasIdentifier || currentHasIdentifier;
+ boolean currentHasIdentifier = addElementsOfAClass(
+ elements, propertyHolder, isPropertyAnnotated,
+ accessType, clazz, mappings
+ );
+ hasIdentifier = hasIdentifier || currentHasIdentifier;
+ }
}
+ catch ( AnnotationException e ) {
+ exceptionWhileWalkingElements = e;
+ }
+ //TODO remember why it should be !inheritanceState.hasParents
if ( !hasIdentifier && !inheritanceState.hasParents ) {
- if ( isExplicitPropertyAnnotated != null ) return null; //explicit but no @Id
+ if ( isExplicitPropertyAnnotated != null ) {
+ //the original exception is legitimate
+ if ( exceptionWhileWalkingElements != null) throw exceptionWhileWalkingElements;
+ return null; //explicit but no @Id: the upper layer will raise an exception
+ }
isPropertyAnnotated = !isPropertyAnnotated;
accessType = "field";
elements.clear();
@@ -873,6 +886,10 @@
hasIdentifier = hasIdentifier || currentHasIdentifier;
}
}
+
+ //the field show no id, fallback tot he original exception
+ if (!hasIdentifier && exceptionWhileWalkingElements != null) throw exceptionWhileWalkingElements;
+
//TODO set the access type here?
entityBinder.setPropertyAnnotated( isPropertyAnnotated );
entityBinder.setPropertyAccessor( accessType );
@@ -1004,7 +1021,7 @@
log.debug( "Processing " + propertyHolder.getEntityName() + " " + accessType + " annotation" );
List<XProperty> properties = annotatedClass.getDeclaredProperties( accessType );
- //order so that property are used int he same order when binding native query
+ //order so that property are used in the same order when binding native query
Collections.sort( properties, new Comparator<XProperty>() {
public int compare(XProperty property1, XProperty property2) {
return property1.getName().compareTo( property2.getName() );
Added: annotations/trunk/src/test/org/hibernate/test/annotations/generics/Dummy.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/generics/Dummy.java (rev 0)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/generics/Dummy.java 2008-03-04 03:37:29 UTC (rev 14385)
@@ -0,0 +1,36 @@
+//$
+package org.hibernate.test.annotations.generics;
+
+import javax.persistence.Transient;
+import javax.persistence.Id;
+import javax.persistence.Entity;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class Dummy<K> {
+
+ @Id
+ private Long id;
+
+ @Transient
+ transient private K dummyField;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public K getDummyField() {
+ return dummyField;
+ }
+
+ public void setDummyField(K dummyField) {
+ this.dummyField = dummyField;
+ }
+
+}
\ No newline at end of file
Modified: annotations/trunk/src/test/org/hibernate/test/annotations/generics/GenericsTest.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/generics/GenericsTest.java 2008-03-04 02:07:51 UTC (rev 14384)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/generics/GenericsTest.java 2008-03-04 03:37:29 UTC (rev 14385)
@@ -54,7 +54,10 @@
PaperType.class,
SomeGuy.class,
Price.class,
- WildEntity.class
+ WildEntity.class,
+
+ //test at deployment only test unbound property when default field access is used
+ Dummy.class
};
}
}
Deleted: annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOne.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOne.java 2008-03-04 02:07:51 UTC (rev 14384)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/idmanytoone/IdManyToOne.java 2008-03-04 03:37:29 UTC (rev 14385)
@@ -1,72 +0,0 @@
-//$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 IdManyToOne 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();
- s.close();
- }
-
- protected Class[] getMappings() {
- return new Class[] {
- Store.class,
- Customer.class,
- StoreCustomer.class,
- CardKey.class,
- CardField.class,
- Card.class,
- Project.class
- };
- }
-}
16 years, 8 months
Hibernate SVN: r14384 - in annotations/trunk/src: test/org/hibernate/test/annotations/idmanytoone and 1 other directory.
by hibernate-commits@lists.jboss.org
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;
+ }
+
+}
+
16 years, 8 months
Hibernate SVN: r14383 - annotations/trunk/src/java/org/hibernate/cfg.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2008-03-03 19:22:13 -0500 (Mon, 03 Mar 2008)
New Revision: 14383
Modified:
annotations/trunk/src/java/org/hibernate/cfg/AnnotationConfiguration.java
annotations/trunk/src/java/org/hibernate/cfg/FkSecondPass.java
Log:
ANN-683 avoid hashCode collisions for FKSecondPass (IBM VM reuse the same hashCode between different objects
Modified: annotations/trunk/src/java/org/hibernate/cfg/AnnotationConfiguration.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/AnnotationConfiguration.java 2008-02-29 17:14:51 UTC (rev 14382)
+++ annotations/trunk/src/java/org/hibernate/cfg/AnnotationConfiguration.java 2008-03-04 00:22:13 UTC (rev 14383)
@@ -401,7 +401,6 @@
compare = -1;
}
else if ( f1.hashCode() == f2.hashCode() ) {
- //FIXME (do not use the hashCode as two objects can have identical hashCode values)
compare = 0;
}
else {
Modified: annotations/trunk/src/java/org/hibernate/cfg/FkSecondPass.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/FkSecondPass.java 2008-02-29 17:14:51 UTC (rev 14382)
+++ annotations/trunk/src/java/org/hibernate/cfg/FkSecondPass.java 2008-03-04 00:22:13 UTC (rev 14383)
@@ -1,6 +1,8 @@
//$Id: FkSecondPass.java 11650 2007-06-07 16:44:52Z epbernard $
package org.hibernate.cfg;
+import java.util.concurrent.atomic.AtomicInteger;
+
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Value;
@@ -10,16 +12,44 @@
public abstract class FkSecondPass implements SecondPass {
protected SimpleValue value;
protected Ejb3JoinColumn[] columns;
+ /**
+ * unique counter is needed to differenciate 2 instances of FKSecondPass
+ * as they are compared.
+ * Fairly hacky but IBM VM sometimes returns the same hashCode for 2 different objects
+ * TODO is it doable to rely on the Ejb3JoinColumn names? Not sure at they could be inferred
+ */
+ private int uniqueCounter;
+ private static AtomicInteger globalCounter = new AtomicInteger();
public FkSecondPass(SimpleValue value, Ejb3JoinColumn[] columns) {
this.value = value;
this.columns = columns;
+ this.uniqueCounter = globalCounter.getAndIncrement();
}
+ public int getUniqueCounter() {
+ return uniqueCounter;
+ }
+
public Value getValue() {
return value;
}
+ public boolean equals(Object o) {
+ if ( this == o ) return true;
+ if ( !( o instanceof FkSecondPass ) ) return false;
+
+ FkSecondPass that = (FkSecondPass) o;
+
+ if ( uniqueCounter != that.uniqueCounter ) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return uniqueCounter;
+ }
+
public abstract String getReferencedEntityName();
public abstract boolean isInPrimaryKey();
16 years, 8 months