[
http://opensource.atlassian.com/projects/hibernate/browse/HHH-3824?page=c...
]
Sharath Reddy commented on HHH-3824:
------------------------------------
This seems to work for me, testing against branch 3.5. I created classes similar to those
provided by Satish, the reporter of this issue:
@Entity
class Item {
@Id
@GeneratedValue
Long id;
@Column
String sku;
@OneToOne(optional=true)
@JoinColumn(name = "sku", referencedColumnName = "sku",
insertable=false, updatable=false)
Inventory inventory;
}
@Entity
public class Inventory implements Serializable {
@Id
@GeneratedValue
Long id;
@Column
String sku;
@Column
Long quantity;
}
Note that, as mentioned in the Hibernate docs, this class implements Serializable. This is
a requirement when there is a join to a non-primary key column.
Test case:
public void testOneToOneJoinByNonprimaryKeyColumn() {
Session s = openSession(new JoinCounter(1));
Transaction tx = s.beginTransaction();
Item item = new Item();
item.sku = "SKU";
Inventory inventory = new Inventory();
inventory.quantity = 1000L;
inventory.sku = "SKU";
s.persist(inventory );
s.persist(item);
s.flush();
s.clear();
item = ( Item ) s.get( Item.class, item.id );
assertNotNull( item );
assertNotNull( item.inventory );
assertEquals( "SKU", item.inventory.sku );
s.flush();
s.clear();
tx.rollback();
s.close();
}
Generated SQL:
select
item0_.id as id0_1_,
item0_.sku as sku0_1_,
inventory1_.id as id1_0_,
inventory1_.quantity as quantity1_0_,
inventory1_.sku as sku1_0_
from
Item item0_
left outer join
Inventory inventory1_
on item0_.sku=inventory1_.sku
where
item0_.id=?
OneToOne join by Non-primary key column throws
PropertyAccessException when join column is String
-------------------------------------------------------------------------------------------------
Key: HHH-3824
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-3824
Project: Hibernate Core
Issue Type: Bug
Components: query-criteria, query-hql
Affects Versions: 3.3.1
Environment: Microsoft SQL server 2005, Hibernate 3.3.1
Reporter: sathish
Attachments: hibernate-3.5.0-beta-3-annotations.patch,
hibernate-3.5.0-beta-3-core.patch, trace.txt
When i try to join 2 entities by OneToOne using a Non-primary key column, it throws the
following exception:
org.hibernate.PropertyAccessException: could not get a field value by reflection getter
of com.xxx.domain.Inventory.sku
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field
com.inwk.estore.server.domain.Inventory.sku to java.lang.Integer
[Full stacktrace attached]
sku is the Non-primary key String join column by which im trying to do a OneToOne join
between Item and Inventory entity.
Apparently its trying to interpret Sku as an Integer column. Adding columnDefinition as
VARCHAR/String doesn't help either
Mappings for Item and Inventory are below:
class Item{
@Id
Long id;
@Column
String sku;
//some more properties
@OneToOne(optional = true)
@JoinColumn(name = "sku", referencedColumnName = "sku", insertable =
false, updatable = false)
Inventory inventory;
}
class Inventory{
@Id
Long id
@Column
String sku
@Column
Long quantity;
}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira