JPA id null (field doesn't seem to be, being autogenerated)

1,859 views
Skip to first unread message

chris 0

unread,
Dec 6, 2011, 7:54:50 PM12/6/11
to play-fr...@googlegroups.com
When I instantiate the following entity, the id field doesn't seem to be auto-generated.

I instantiate like:
 Payment payment = new Payment(transaction,status,payer,itemname,itemid);
 System.out.println("ID________"+payment.getId());
The above prints null.

When I try to get the object from the database and print the ID that also prints null.

@Entity
public class Payment extends Model {

   
    @Column(unique=true, nullable=false)
    public String transaction;
   
    public String status;
   
    public String payer;
   
    public String itemname;
   
    public String itemnumber;
   
    public Payment(String transaction,String status,String payer,String itemname,String itemnumber){
        this.transaction = transaction;
        this.payer = payer;
        this.itemname = itemname;
        this.itemnumber = itemnumber;
   
    }
}

thanks


Chris

Santiago

unread,
Dec 7, 2011, 6:50:42 AM12/7/11
to play-framework
There is no id until the entity is saved into the database.

Try this:

Model payment = new
Payment(transaction,status,payer,itemname,itemid).save();
System.out.println("ID________"+payment.getId());

And this:

Model p = Payment.findById(payment.getId());
System.out.println("ID________"+p.getId());

Regards,
Santiago

chris 0

unread,
Dec 7, 2011, 11:40:42 AM12/7/11
to play-fr...@googlegroups.com
I'm having problems when attempting to read the object back:

When I try to read back the object using .find(), the object doesn't have an ID.
So I get an exception thrown by the second .save() command.

My code is now:

    Payment p = new Payment("trans3","Complete","deutr...@gmail.com","item cloud","xxxxx");
           
            try {
                p.save();
                System.out.println("ID::::::::::::: "+p.getId());  // Actually prints a correct ID
            } catch(PersistenceException e){
                List <Payment> p2 = Payment.find("transaction","trans3").fetch();
                Payment pay = p2.get(0);
                System.out.println("Read transaction_____"+pay.transaction+" id: "+p.getId());
                pay.status = "Complete2";
                pay.save();
            }

Read transaction_____trans3 id: null
16:36:22,400 ERROR ~ an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null id in models.Payment entry (don't flush the Session after an exception occurs)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:240)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
    at play.db.jpa.JPABase._save(JPABase.java:47)
    at play.db.jpa.GenericModel.save(GenericModel.java:184)
    at controllers.Application.index(Application.java:35)
    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:597)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:546)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:500)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:476)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:471)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:159)
    at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:220)
    at play.Invoker$Invocation.run(Invoker.java:265)
    at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:200)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
16:36:22,678 ERROR ~

@68jfoidm3
Internal Server Error (500) for request GET /

Execution exception (In /app/controllers/Application.java around line 35)
AssertionFailure occured : null id in models.Payment entry (don't flush the Session after an exception occurs)


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


Santiago Gilabert

unread,
Dec 7, 2011, 1:28:47 PM12/7/11
to play-fr...@googlegroups.com
Can you send the stacktrace for PersistenceException ? The one you get on the first save() ...

chris 0

unread,
Dec 7, 2011, 1:43:13 PM12/7/11
to play-fr...@googlegroups.com
Sure the code is now:

           String trans="transaction9000";
           Payment p = new Payment(trans,"Complete","em...@gmail.com","item ","item id");
           
            try {
                p.save();
            }catch(PersistenceException e){
                List <Payment> p2 = Payment.find("transaction",trans).fetch();
                Payment pay1 = p2.get(0);
                pay1.itemname = "updating item number";
                pay1.save();
            }
           

It works when i run this code the first time, when i run it the second time
and it enters the catch statement, the error occurs on the 2nd .save().

The complete error trace is:

18:41:34,090 WARN  ~ SQL Error: 23001, SQLState: 23001
18:41:34,091 ERROR ~ Unique index or primary key violation: "CONSTRAINT_INDEX_F ON PUBLIC.PAYMENT(TRANSACTION)"; SQL statement:
insert into Payment (id, itemname, itemnumber, payer, status, transaction) values (null, ?, ?, ?, ?, ?) [23001-149]
18:41:34,291 ERROR ~ an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
18:41:34,547 ERROR ~

@68jgf3o30

Internal Server Error (500) for request GET /

Execution exception (In /app/controllers/Application.java around line 35)
AssertionFailure occured : null id in models.Payment entry (don't flush the Session after an exception occurs)

play.exceptions.JavaExecutionException: null id in models.Payment entry (don't flush the Session after an exception occurs)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:229)
    at Invocation.HTTP Request(Play!)
Caused by: org.hibernate.AssertionFailure: null id in models.Payment entry (don't flush the Session after an exception occurs)

    at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:240)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
    at play.db.jpa.JPABase._save(JPABase.java:47)
    at play.db.jpa.GenericModel.save(GenericModel.java:184)
    at controllers.Application.index(Application.java:35)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:546)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:500)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:476)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:471)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:159)
    ... 1 more
Reply all
Reply to author
Forward
0 new messages