-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate And Primary Key Issue
PostPosted: Wed Sep 12, 2007 9:00 am 
Newbie

Joined: Tue Dec 20, 2005 5:47 pm
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Mapping documents:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
   <class name="com.dowhi.table.mapping.OrderLines" table="orderlines">
      <id name="OrderInfoId" column="orderinfo_id" type="java.lang.Long">
         <generator class="sequence">
            <param name="sequence">orderlines_orderinfo_id_seq</param>
         </generator>
      </id>
      
      <property name="ItemId" column="item_id" type="java.lang.Long" />
      <property name="Quantity" column="quantity" type="java.lang.Integer" />
      <property name="Price" column="price" type="java.lang.Double" />
      <property name="Discount" column="discount" type="java.lang.Integer" />
      <property name="SubTotal" column="subtotal" type="java.lang.Double" />
      <property name="VAT" column="vat" type="java.lang.Double" />
      <property name="Total" column="total" type="java.lang.Double" />
   </class>


Code between sessionFactory.openSession() and session.close():
Code:
long orderinfo_id = oi.getOrderInfoId();
         Session session = sessionFactory.openSession();
         for(int i=0; i<2; i++) {
            OrderLines ol = new OrderLines();
            ol.setOrderInfoId(orderinfo_id);
            ol.setItemId(i);
            ol.setQuantity(1);
            ol.setPrice(9.99d);
            ol.setDiscount(10);
            ol.setSubTotal(8.99d);
            ol.setVAT(1.00d);
            ol.setTotal(11.75);
            
            try {
               session.beginTransaction();
               session.save(ol);
               session.getTransaction().commit();
               session.flush();
               session.clear();
            } catch (HibernateException ex) {
               ex.printStackTrace();
               
            }
         }


Full stack trace of any exception that occurs:
Code:
org.hibernate.exception.GenericJDBCException: could not get next sequence value
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:96)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
   at com.dowhi.Test.main(Test.java:109)
Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
   at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1548)
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1316)
   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:191)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:351)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:255)
   at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:75)
   ... 10 more


Name and version of the database you are using: Postgresql 8.0
Code:
CREATE TABLE ORDERLINES (
orderinfo_id bigint not null,
item_id bigint not null,
quantity integer not null,
price numeric(7, 2) not null,
discount integer not null default 0,
subtotal numeric(7, 2) not null default 0.00,
vat numeric(7, 2) default 0.00,
total numeric(7, 2) not null default 0.00,
CONSTRAINT oderlines_pk PRIMARY KEY(orderinfo_id, item_id),
CONSTRAINT oderlines_oderinfo_id_fk FOREIGN KEY(orderinfo_id) REFERENCES
  orderinfos(orderinfo_id),
CONSTRAINT orderlines_item_id_fk FOREIGN KEY(item_id) REFERENCES
  items(item_id)
);


Compiler is complaining about not being able to get the next sequence. So, logically it has something to do with the primary key. If you look at the orderlines table, the primary key consists of orderinfo_id and item_id. How am I to map this using hibernate?

Lots of thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 12, 2007 7:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Did you actually create the sequence in the DB?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 12, 2007 8:08 pm 
Newbie

Joined: Tue Dec 20, 2005 5:47 pm
Posts: 13
Thanks David,

For replying back. The answer to your question is NO. I have had look at the sequence tables and non have been generated. However, I have expected this as orderinfo_id and item_id columns have not been defined as serial. Of course this is how I want it.

Correct me if I am wrong, I have the idea that I cannot map a table using hibernate without an serial id as a primary key?!

If I can than how can I map this following test table for example in hibernate: (I am using Postgresql Database 8.1)

Code:
CREATE TABLE TEST (
first_id integer not null,
second_id integer not null,
CONSTRAINT test_pk PRIMARY KEY(first_id, second_id)
)


Thanks again!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.