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.0Code:
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.