I'm trying to map a one-to-one relationship between Person and Adress. Person is an abstract class. Theirs generalizations are Client and Vendor.
Adress have a one-to-one relationship with both Client and Vendor class.
My class model is something like that:
ignore the points
............ 1_________1 Client _______|\
Adress.................................................|/
............ 1_________1................................ Person
..................................Vendor_______|\
...........................................................|/
I've one table for each class (Table for subclass)
public abstract class Person {
private Int codePers;
....}
public class Cliente extends Person {
private char classification;
private Adress adress;
....}
public class Vendedor extends Person {
private Integer bonus;
private Adress adress;
....}
public class Adress {
private int codePers;
private Client client;
private Vendor vendor;
....}
I map the inhiritance between Person, Client and Vendor as the follow:
<class name="dto.Person" table="PERSON">
<id name="codigoPers" column="CODIGO_PERS" type="java.lang.Integer" unsaved-value="0" >
<generator class="increment"/>
</id>
<joined-subclass name="dto.Client"
table="CLIENT">
<key column="CODIGO_CLI"/>
<property
name="classification"
type="java.lang.Character"
/>
</joined-subclass>
<joined-subclass name="dto.Vendor"
table="VENDOR">
<key column="CODIGO_VEND"/>
<property
name="bonus"
type="java.lang.Integer"
/>
</joined-subclass>
</class>
it's working correctly.
I map the relationship between Person (Client and Vendor) and Adress as the follow:
<class name="dto.Adress"
table="adress">
<id name="codigoPers" column="CODIGO_PERS" type="java.lang.Integer" unsaved-value="0" >
<generator class="foreign">
<param name="property">dto.Person</param>
</generator>
</id>
<one-to-one name="client"
class="dto.Client"
constrained="true"/>
<one-to-one name="vendor"
class="dto.Vendor"
constrained="true"/>
</class>
I'm retrieving a collection of Adress (from Adress Order by Codigo_Pers).
If I use
adress.getVendor().getCodigoPers()
and
adress.getCleint().getCodigoPers()
separate, in distinct moments, it works.
if I try to get both Client and Vendor of the same instance of Adress i receive an error:
Here follow the output of ocnsole:
Hibernate: select adress0_.CODIGO_PERS as CODIGO1_3_ from adress adress0_ order by Codigo_Pers
2006-09-27 10:42:56,491 WARN [org.hibernate.engine.StatefulPersistenceContext] - <Narrowing proxy to class dto.Vendedor - this operation breaks ==>
Hibernate: select vendor0_.CODIGO_VEND as CODIGO1_4_0_ from VENDOR vendor0_, PERSON vendor0_1_ where vendor0_.CODIGO_VEND=vendor0_1_.CODIGO_PERS and vendor0_.CODIGO_VEND=?
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: dto.Vendor
at dto.Client$$FastClassByCGLIB$$654364cb.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:161)
at dto.Client$$EnhancerByCGLIB$$b6b343ce.getClassification(<generated>)
at view.Simulador.jButton1ActionPerformed(Simulator.java:139)
at view.Simulador.access$0(Simulator.java:67)
at view.Simulador$1.actionPerformed(Simulator.java:44)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
How can I fix the problem?
I can send more details and until the project if necessary.
|