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.