Skip to main content

Ejb3 :problem with insert( table that has autogenerated primary key)

2 replies [Last post]
shruthifcg
Offline
Joined: 2007-05-01

hi
i am working on EJB3.
i am trying to insert object into table.Primary key of table will be generated automatically.I tried following statement for insertion,

em.persistent(customerObj);

but it throw following exception,

javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): customer.CustomerInfo
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:102)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:211)
at org.jboss.ejb3.stateless.Stateles

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zerok
Offline
Joined: 2005-03-30

Hi,

I use to have this same problem when I work with composite Ids.
Check your primary key property. It may be a composite class with two or more properties inside.

It is possible that these properties represent the very foreign keys from other objects your class has. So you may be setting them as properties of your class, but forgot to populate your primary key.

So lets say the class you are trying to persist is:

class A {
CompositeKey composite;
B b;
C c;
}

class CompositeKey {
Integer idB;
Integer idC;
}

class B {
Integer idB;
String name;
}

class C {
Integer idC;
String name;
}

You may have done:
A a = new A();
B b = someBusinessRule.getB();
C c = someBusinessRule.getC();
a.setB( b );
a.setC( c );

but forgot to do:
CompositeKey comp = new CompositeKey();
comp.setIdB( b.getIdB() );
comp.setIdC( c.getIdC() );
a.setComposite( comp );

Hope it helps you. Let me know if so.

Bye

shruthifcg
Offline
Joined: 2007-05-01

well i have resovled that problem.
the problem is not composite key.
the problem is Myeclipse IDE.Actually Entity bean is auto geneated by IDE.
In that entity bean one annotation was missing.
i.e. @GeneratedValue(strategy=GenerationType.SEQUENCE) i included this annotation in my entity bean ,now it works perfectly.