-->
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: JPA 2 Derived Identifiers
PostPosted: Tue Aug 02, 2011 4:00 am 
Newbie

Joined: Tue Aug 02, 2011 3:39 am
Posts: 1
Hi guys,

I have following POJO:

Code:
/**----------------------------------------parent object ----------------------------------------------------------------------------**/
@Entity
@Table(name = "FRAUD")
public class Fraud {

    @Id
    @Column(name = "FRAUD_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FRAUD_SEQ")
    @SequenceGenerator(name = "FRAUD_SEQ", sequenceName = "FRAUD_SEQ")
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "id")
    private List<FraudVersion> versions = new ArrayList<FraudVersion>();


    public void addVersions(FraudVersion version) {
        if (version.getId() != this) {
            this.versions.add(version);
            version.setId(this);
        }
    }
     // get set hashCode equals ......
}

/**-----------------------------------------child object------------------------------------------------------------------------------**/

@Entity
@Table(name = "FRAUD_VERSION")
@IdClass(FraudVersionPK.class)
public class FraudVersion implements Serializable {

    @Id
    private Long version;

    @Id
    @ManyToOne
    @JoinColumn(name = "FRAUD_ID")
    private Fraud id;
    // get set hasCode equals
}

/**---------------------------------------------------- PK object -------------------------------------------------------------------**/
public class FraudVersionPK implements Serializable {

    @Column(name = "FRAUD_ID")
    private Long id;

    @Column(name = "FRAUD_VERSION")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FRAUD_VERSION_SEQ")
    @SequenceGenerator(name = "FRAUD_VERSION_SEQ", sequenceName = "FRAUD_VERSION_SEQ")
    private Long version;
    //get set hasCode equals
}
/**-----------------------------------------------------------------------------------------------------------------------------------**/


With hbm2ddl tables and constraints generated as expected , but when I try to persist parent class with below code:
Code:
           
Fraud fraud = new Fraud();
FraudVersion version = new FraudVersion();
fraud.addVersions(version);
service.save(fraud);


It failed with below error:
"Can not set java.lang.Long field FraudVersionPK.id to Fraud" which means Hibernate is trying to pass parent object to it's @Id object, may I know why this is happening with current annotation ? And how to solve it ?

I am using hibernate 3.5.1 Final and with @Embeddable I am encountering a same error.

Thank you all for the time reading this post.


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.