Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:core 3.2.3, annotations 3.3.0, entity manager 3.3.1, JPA 1.0
Mapping documents:using anotation
Full stack trace of any exception that occurs:
Name and version of the database you are using: Oracle 10g
I have the following relationship I want to model. a Client has many Contacts. The Contact has a compound key: id and Client's id.
The compound key class is:
Code:
@Embeddable
public class ContactKey implements Serializable {
/**
* eclipse generated
*/
private static final long serialVersionUID = 3787146968072373925L;
/**
* @TODO Entity: needs to replace the temp sequence
*/
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "test_seq")
@SequenceGenerator(name = "test_seq", sequenceName = "test_s", allocationSize=1)
@Column(name="CNTCT_ID")
private Integer id;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="CLNT_ID")
private Client client;
.....
On the Contact class side:
@Entity
@Table(name="CNTCT")
public class Contact {
@EmbeddedId
private ContactKey key;
.....
On the Client class side:
@Entity
@Table(name="CLNT")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "test_seq")
@SequenceGenerator(name = "test_seq", sequenceName = "test_s", allocationSize=1)
@Column(name="CLNT_ID")
private Integer id;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="key.client")
private Set<Contact> contacts;
......
On the Test class:
Client client = new Client();
Contact contact = new Contact();
contact.setClient(client);
client.addContact(contact);
then save
I expect hibernate to generate the id for ContactKey. Instead, I got an error:
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("DEVELOPER"."CNTCT"."CNTCT_ID")
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10698)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
Is my annotation wrong? I have used compound key before successfully. This is the first time I need to generate a sequence value for part of it.