Author: epbernard
Date: 2007-02-10 02:27:14 -0500 (Sat, 10 Feb 2007)
New Revision: 11186
Added:
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/Carz.java
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/Lotz.java
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/LotzPK.java
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/ManyToOneOnNonPkTest.java
Modified:
branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/BinderHelper.java
branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/Ejb3JoinColumn.java
Log:
ANN-548 investigate and set a unit test. Fix part of the issue, the rest is close to
impossible
Modified:
branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/BinderHelper.java
===================================================================
---
branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/BinderHelper.java 2007-02-10
03:51:57 UTC (rev 11185)
+++
branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/BinderHelper.java 2007-02-10
07:27:14 UTC (rev 11186)
@@ -222,24 +222,17 @@
orderedColumns.add( column );
columnsToProperty.put( column, new HashSet<Property>() );
}
- Iterator it = columnOwner instanceof PersistentClass ?
+ boolean isPersistentClass = columnOwner instanceof PersistentClass;
+ Iterator it = isPersistentClass ?
( (PersistentClass) columnOwner ).getPropertyIterator() :
( (Join) columnOwner ).getPropertyIterator();
while ( it.hasNext() ) {
- Property property = (Property) it.next();
- if ( "noop".equals( property.getPropertyAccessorName() )
- || "embedded".equals( property.getPropertyAccessorName() ) ) {
- continue;
- }
- Iterator columnIt = property.getColumnIterator();
- while ( columnIt.hasNext() ) {
- Object column = columnIt.next(); //can be a Formula so we don't cast
- //noinspection SuspiciousMethodCalls
- if ( columnsToProperty.containsKey( column ) ) {
- columnsToProperty.get( column ).add( property );
- }
- }
+ matchColumnsByProperty( (Property) it.next(), columnsToProperty );
}
+ if (isPersistentClass) {
+ matchColumnsByProperty( ( (PersistentClass) columnOwner ).getIdentifierProperty(),
columnsToProperty );
+ }
+
//first naive implementation
//only check 1 columns properties
//TODO make it smarter by checking correctly ordered multi column properties
@@ -258,6 +251,31 @@
return orderedProperties;
}
+ private static void matchColumnsByProperty(Property property, Map<Column,
Set<Property>> columnsToProperty) {
+ if ( property == null ) return;
+ if ( "noop".equals( property.getPropertyAccessorName() )
+ || "embedded".equals( property.getPropertyAccessorName() ) ) {
+ return;
+ }
+// FIXME cannot use subproperties becasue the caller needs top level properties
+// if ( property.isComposite() ) {
+// Iterator subProperties = ( (Component) property.getValue() ).getPropertyIterator();
+// while ( subProperties.hasNext() ) {
+// matchColumnsByProperty( (Property) subProperties.next(), columnsToProperty );
+// }
+// }
+ else {
+ Iterator columnIt = property.getColumnIterator();
+ while ( columnIt.hasNext() ) {
+ Object column = columnIt.next(); //can be a Formula so we don't cast
+ //noinspection SuspiciousMethodCalls
+ if ( columnsToProperty.containsKey( column ) ) {
+ columnsToProperty.get( column ).add( property );
+ }
+ }
+ }
+ }
+
/**
* Retrieve the property by path in a recursive way, including IndetifierProperty in the
loop
* If propertyName is null or empty, the IdentifierProperty is returned
Modified:
branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/Ejb3JoinColumn.java
===================================================================
---
branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/Ejb3JoinColumn.java 2007-02-10
03:51:57 UTC (rev 11185)
+++
branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/Ejb3JoinColumn.java 2007-02-10
07:27:14 UTC (rev 11186)
@@ -391,6 +391,7 @@
boolean contains = idColumns.contains( refCol );
if ( ! contains ) {
isFkReferencedColumnName = true;
+ break; //we know the state
}
}
}
@@ -400,7 +401,10 @@
else if ( noReferencedColumn ) {
return NO_REFERENCE;
}
- else {
+ else if ( idColumns.size() != columns.length ) {
+ //reference use PK but is a subset or a superset
+ return NON_PK_REFERENCE;
+ } else {
return PK_REFERENCE;
}
}
Added:
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/Carz.java
===================================================================
---
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/Carz.java
(rev 0)
+++
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/Carz.java 2007-02-10
07:27:14 UTC (rev 11186)
@@ -0,0 +1,124 @@
+//$Id: $
+package org.hibernate.test.annotations.manytoone;
+
+import java.io.Serializable;
+import java.util.Date;
+import javax.persistence.Entity;
+import javax.persistence.ManyToOne;
+import javax.persistence.Column;
+import javax.persistence.JoinColumns;
+import javax.persistence.FetchType;
+import javax.persistence.JoinColumn;
+import javax.persistence.TemporalType;
+import javax.persistence.Id;
+import javax.persistence.Temporal;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class Carz implements Serializable {
+ @Id
+ private Integer id;
+
+ @Column( name = "make", nullable = false )
+ private String make;
+
+ @Column( name = "model", nullable = false )
+ private String model;
+
+ @Column( name = "manufactured", nullable = false )
+ @Temporal( TemporalType.TIMESTAMP )
+ private Date manufactured;
+
+ @ManyToOne( fetch = FetchType.LAZY )
+ @JoinColumn( name = "loc_code", referencedColumnName = "loc_code" )
+ private Lotz lot;
+
+ public Carz() {
+ }
+
+ public Integer getId() {
+ return this.id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Lotz getLot() {
+ return this.lot;
+ }
+
+ public void setLot(Lotz lot) {
+ this.lot = lot;
+ }
+
+ public String getMake() {
+ return this.make;
+ }
+
+ public void setMake(String make) {
+ this.make = make;
+ }
+
+ public Date getManufactured() {
+ return this.manufactured;
+ }
+
+ public void setManufactured(Date manufactured) {
+ this.manufactured = manufactured;
+ }
+
+ public String getModel() {
+ return this.model;
+ }
+
+ public void setModel(String model) {
+ this.model = model;
+ }
+
+ @Override
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ result = PRIME * result + ( ( this.id == null ) ?
+ 0 :
+ this.id.hashCode() );
+ result = PRIME * result + ( ( this.make == null ) ?
+ 0 :
+ this.make.hashCode() );
+ result = PRIME * result + ( ( this.manufactured == null ) ?
+ 0 :
+ this.manufactured.hashCode() );
+ result = PRIME * result + ( ( this.model == null ) ?
+ 0 :
+ this.model.hashCode() );
+ 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 Carz other = (Carz) obj;
+ if ( this.id == null ) {
+ if ( other.id != null ) return false;
+ }
+ else if ( !this.id.equals( other.id ) ) return false;
+ if ( this.make == null ) {
+ if ( other.make != null ) return false;
+ }
+ else if ( !this.make.equals( other.make ) ) return false;
+ if ( this.manufactured == null ) {
+ if ( other.manufactured != null ) return false;
+ }
+ else if ( !this.manufactured.equals( other.manufactured ) ) return false;
+ if ( this.model == null ) {
+ if ( other.model != null ) return false;
+ }
+ else if ( !this.model.equals( other.model ) ) return false;
+ return true;
+ }
+}
\ No newline at end of file
Added:
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/Lotz.java
===================================================================
---
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/Lotz.java
(rev 0)
+++
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/Lotz.java 2007-02-10
07:27:14 UTC (rev 11186)
@@ -0,0 +1,101 @@
+//$Id: $
+package org.hibernate.test.annotations.manytoone;
+
+import java.util.List;
+import java.io.Serializable;
+import javax.persistence.Entity;
+import javax.persistence.EmbeddedId;
+import javax.persistence.Column;
+import javax.persistence.FetchType;
+import javax.persistence.OneToMany;
+import javax.persistence.CascadeType;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class Lotz implements Serializable {
+ @EmbeddedId
+ protected LotzPK lotPK;
+
+ @Column( name = "name", nullable = false )
+ private String name;
+
+ @Column( name = "location", nullable = false )
+ private String location;
+
+ @OneToMany( mappedBy = "lot", fetch = FetchType.LAZY, cascade =
CascadeType.ALL )
+ private List<Carz> cars;
+
+ public Lotz() {
+ }
+
+ public List<Carz> getCars() {
+ return this.cars;
+ }
+
+ public void setCars(List<Carz> cars) {
+ this.cars = cars;
+ }
+
+ public String getLocation() {
+ return this.location;
+ }
+
+ public void setLocation(String location) {
+ this.location = location;
+ }
+
+ public LotzPK getLotPK() {
+ return this.lotPK;
+ }
+
+ public void setLotPK(LotzPK lotPK) {
+ this.lotPK = lotPK;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ result = PRIME * result + ( ( this.location == null ) ?
+ 0 :
+ this.location.hashCode() );
+ result = PRIME * result + ( ( this.lotPK == null ) ?
+ 0 :
+ this.lotPK.hashCode() );
+ result = PRIME * result + ( ( this.name == null ) ?
+ 0 :
+ this.name.hashCode() );
+ 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 Lotz other = (Lotz) obj;
+ if ( this.location == null ) {
+ if ( other.location != null ) return false;
+ }
+ else if ( !this.location.equals( other.location ) ) return false;
+ if ( this.lotPK == null ) {
+ if ( other.lotPK != null ) return false;
+ }
+ else if ( !this.lotPK.equals( other.lotPK ) ) return false;
+ if ( this.name == null ) {
+ if ( other.name != null ) return false;
+ }
+ else if ( !this.name.equals( other.name ) ) return false;
+ return true;
+ }
+}
Added:
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/LotzPK.java
===================================================================
---
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/LotzPK.java
(rev 0)
+++
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/LotzPK.java 2007-02-10
07:27:14 UTC (rev 11186)
@@ -0,0 +1,34 @@
+//$Id: $
+package org.hibernate.test.annotations.manytoone;
+
+import java.io.Serializable;
+import javax.persistence.Column;
+import javax.persistence.Embeddable;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Embeddable
+public class LotzPK implements Serializable {
+ @Column( name = "id", nullable = false )
+ private Integer id;
+
+ @Column( name = "loc_code", nullable = false, unique = true )
+ private String locCode;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getLocCode() {
+ return locCode;
+ }
+
+ public void setLocCode(String locCode) {
+ this.locCode = locCode;
+ }
+}
Added:
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/ManyToOneOnNonPkTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/ManyToOneOnNonPkTest.java
(rev 0)
+++
branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/manytoone/ManyToOneOnNonPkTest.java 2007-02-10
07:27:14 UTC (rev 11186)
@@ -0,0 +1,52 @@
+//$Id: $
+package org.hibernate.test.annotations.manytoone;
+
+import java.util.Date;
+
+import org.hibernate.test.annotations.TestCase;
+import org.hibernate.Session;
+
+
+/**
+ * FIXME test for ANN-548
+ * @author Emmanuel Bernard
+ */
+public class ManyToOneOnNonPkTest extends TestCase {
+
+ public void testNonPkPartOfPk() throws Exception {
+// Session s = openSession( );
+// s.getTransaction().begin();
+//
+// LotzPK pk = new LotzPK();
+// pk.setId( 1 );
+// pk.setLocCode( "fr" );
+// Lotz lot = new Lotz();
+// lot.setLocation( "France" );
+// lot.setName( "Chez Dede" );
+// lot.setLotPK( pk );
+// Carz car = new Carz();
+// car.setId( 1 );
+// car.setLot( lot );
+// car.setMake( "Citroen" );
+// car.setManufactured( new Date() );
+// car.setModel( "C5" );
+// s.persist( lot );
+// s.persist( car );
+//
+// s.clear();
+//
+// car = (Carz) s.createQuery( "from Carz car left join fetch
car.lot").uniqueResult();
+// assertNotNull( car.getLot() );
+//
+// s.getTransaction().commit();
+// s.close();
+//
+ }
+
+ protected Class[] getMappings() {
+ return new Class[] {
+ //Carz.class,
+ //Lotz.class
+ };
+ }
+}
Show replies by date