Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ejb3 primary key generation does not work without automatic schema generation

 
vidya mahavadi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh.. sorry! can the administrators move the topic to 'Object Relational Mapping Forum'
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vidya mahavadi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the last sentence on my last reply. What is your script you manually do to create your table?

Mark
 
vidya mahavadi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark,

That fixed the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic