-->
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.  [ 2 posts ] 
Author Message
 Post subject: Compound Key with sequence generator problem
PostPosted: Tue May 08, 2007 5:59 pm 
Newbie

Joined: Mon Mar 05, 2007 12:01 pm
Posts: 3
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 11, 2007 9:37 am 
Newbie

Joined: Wed Jul 11, 2007 9:25 am
Posts: 5
Hi,

I'm having the same problem as you. Did you already got a solution?

Composite keys with sequences works fine in Toplink Essentials, with similar code as you have posted, but I need this working in Hibernate.

Alexandre


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

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.