File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes ejb3 primary key generation does not work  without automatic schema generation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "ejb3 primary key generation does not work  without automatic schema generation" Watch "ejb3 primary key generation does not work  without automatic schema generation" New topic
Author

ejb3 primary key generation does not work without automatic schema generation

vidya mahavadi
Ranch Hand

Joined: Nov 24, 2004
Posts: 34
Hi,

I am using EJB3 with Jboss 4.0.5GA and Sybase. I have couple of entity beans which I want to persist.

Here is the part of the entity bean and persistence.xml..

@Entity(name = "Amendment")
@Table
public class Amendment implements Serializable {

private Long amendmentId;
private String assignedTo;
private String portfolioName;
private String shortName;
private int tradeRefNo;

@Id
@GeneratedValue
public Long getAmendmentId() {
return amendmentId;
}

// geetters and setters included
}

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">

<persistence-unit name="amendments_ejbPU">
<description>Amendments persistence</description>
<jta-data-source>java:jdbc/dealAmendmentsDS</jta-data-source>
<class>za.co.rmb.amendments.bean.Amendment</class>
<class>za.co.rmb.amendments.bean.FieldAmendment</class>
<class>za.co.rmb.amendments.bean.xml.XPathMapping</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>

</persistence-unit>
</persistence>

These entities are persisted perfectly when I allow JPA to create the tables in sybase. If I create the tables using a script (with exactly same structure) it does not generate primary key for the rows while inserting.. It throws the following exception.

Caused by: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException
: could not insert: [za.co.rmb.amendments.bean.Amendment]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerIm
pl.java:622)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.j
ava:175)
at za.co.rmb.amendments.bean.AmendmentsServiceBean.saveDealAmendment(AmendmentsServiceBean.java:
139)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.jav
a:63)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersist
enceContextPropagationInterceptor.java:57)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntit
yManagerInterceptor.java:54)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:46)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
... 47 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [za.co.rmb.amendments
.bean.Amendment]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.jav
a:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:20
93)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:25
73)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:47)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventLis
tener.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:
180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListen
er.java:108)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventList
ener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.jav
a:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.jav
a:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:212)
... 64 more
Caused by: com.sybase.jdbc2.jdbc.SybSQLException: The column amendmentId in table Amendment does not all
ow null values.


at com.sybase.jdbc2.tds.Tds.processEed(Tds.java:2834)
at com.sybase.jdbc2.tds.Tds.nextResult(Tds.java:2156)
at com.sybase.jdbc2.jdbc.ResultGetter.nextResult(ResultGetter.java:69)
at com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:220)
at com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:203)
at com.sybase.jdbc2.jdbc.SybStatement.executeLoop(SybStatement.java:1766)
at com.sybase.jdbc2.jdbc.SybStatement.execute(SybStatement.java:1758)
at com.sybase.jdbc2.jdbc.SybPreparedStatement.execute(SybPreparedStatement.java:619)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.execute(WrappedPreparedStatement.jav
a:209)
at org.hibernate.id.IdentityGenerator$InsertSelectDelegate.executeAndExtract(IdentityGenerator.j
ava:108)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.jav
a:33)

does anyone have a work around to this problem.

Regards,
Vidya
vidya mahavadi
Ranch Hand

Joined: Nov 24, 2004
Posts: 34
Oh.. sorry! can the administrators move the topic to 'Object Relational Mapping Forum'
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Of course, I can move it.

The big question is what is Sybase's default ID generator, is it a sequence or an auto-increment field.

If it is a sequence, and since you don't name the sequence it will look for a Hibernate sequence table. So in that case, you would need to create the sequence table yourself if you use a script manually.

Or if it is an auto-increment field are you defining the Primary Key as an auto-increment field or are you defining it simply as a number field?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
vidya mahavadi
Ranch Hand

Joined: Nov 24, 2004
Posts: 34
Thanks for the reply Mark!

Sybase does not support sequence generation. So I too the default way

@Id
@GeneratedValue
public Long getAmendmentId() {
return amendmentId;
}

I did not mention (strategy = GenerationType.AUTO), since it is default and did not make any difference. But why is it creating the ids properly when JPA creates the tables and fails when I create using a script??
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Read the last sentence on my last reply. What is your script you manually do to create your table?

Mark
vidya mahavadi
Ranch Hand

Joined: Nov 24, 2004
Posts: 34
Thanks Mark,

That fixed the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ejb3 primary key generation does not work without automatic schema generation