-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping a part of composite key as a foreign key?
PostPosted: Mon Jul 24, 2006 1:54 pm 
Beginner
Beginner

Joined: Mon Dec 05, 2005 4:15 am
Posts: 36
In one of my classes(A), I have a composite ID. This composite ID consists of 2 fields: one String (f1) and one Integer(f2), which is a foreign key to another entity(B) and has a many-to-one relation to the ID of the B entities. (like key-many-to-one property of composite key in the classical hibernate)
My mappling:

Code:
@Entity
@Table(name="ATABLE")
public class A implements java.io.Serializable{
...

@EmbeddedId
   
    @AttributeOverrides( {
        @AttributeOverride(name="f1", column=@Column(name="f1", unique=false, nullable=false, insertable=true, updatable=true, precision=22, scale=0) ),
        @AttributeOverride(name="f2", column=@Column(name="f2", unique=false, nullable=false, insertable=true, updatable=true, precision=22, scale=0) ) } )
    public AId getId() {
        return this.id;
    }

    public void setId(AId id) {
        this.id = id;
    }
...
}


public class AId implements java.io.Serializable {
    private String f1;

    @ManyToOne
    @JoinColumn (name="f2", referencedColumnName="f2")
   private B f2;

    public String getF1() {
        return this.f1;
    }

    public void setF1(String f1) {
        this.f1 = f1;
    }

    public BgetF2() {
        return this.f2;
    }

    public void setF2(B f2) {
        this.f2 = f2;
    }

...

}


@Entity
@Table(name="BTABLE")

public class B implements java.io.Serializable {

@Id
private Integer f2;
...

}


If I try to get an instance of class A, I get an error:

Code:
org.hibernate.type.SerializationException: could not deserialize
        at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:217)
        at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)
        at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:78)
        at org.hibernate.type.SerializableType.get(SerializableType.java:39)
        at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113)
        at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:102)
        at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
        at org.hibernate.type.ComponentType.hydrate(ComponentType.java:506)
        at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:229)
        at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1088)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:554)
        at org.hibernate.loader.Loader.doQuery(Loader.java:689)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
        at org.hibernate.loader.Loader.doList(Loader.java:2145)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
        at org.hibernate.loader.Loader.list(Loader.java:2024)
        at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1562)
        at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
        at mypackage.myDAO.search(Search.java:123)

Caused by: java.io.StreamCorruptedException: invalid stream header
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:753)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
        at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:252)
        at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)
        ... 35 more



If I change the AId Class definition to

Code:
public class AId implements java.io.Serializable {
    private String f1;

    private Integer f2;

    public String getF1() {
        return this.f1;
    }

    public void setF1(String f1) {
        this.f1 = f1;
    }

    public Integer getF2() {
        return this.f2;
    }

    public void setF2(Integer f2) {
        this.f2 = f2;
    }

...

}


then I can get instances of the class A. But how can I map the part of the composite key which is simultaneously a foreign key to another entity with EJB 3.0 Annotations? All my entity classes and the composite ID are implement java.io.Serializable.

Thank you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.