Quantcast

@EJB @GeneratedValue(strategy = GenerationType.SEQUENCE

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

@EJB @GeneratedValue(strategy = GenerationType.SEQUENCE

kriskross
Hi  there.
I have a problem with a entity that I can't figure out. Hope someone can helpe me out.

/**** Class****/
@Entity
public class A implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="A_aId_seq")
    private Integer aId;
    private String name;
}
/***********method addet to AFacadeRemote**********/
private A uppdateMe(A a) {
   return em.merge(a);
}


/********* method ********/
public Integer getAId(String test) {
   AFacadeRemote afr;
   A a = new A(test);
   afr.create(a);
   a = afr.updateMe(a);
   return a.getAId();
}

In my db I now have a record.
But the method returns null;
Any idea how I can create A and return A.getAId() in the same method. It is not possible to create a finder method and search for A.name, name is not unique.

Best regards
Kristian
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @EJB @GeneratedValue(strategy = GenerationType.SEQUENCE

Stephen Kaspersen
You could drop into native SQL and search for the last_insert_id()  for
that table.

Are you using Mysql?
I have found using an autoincrement column and GenerationType.IDENTITY
does update the Entity when its created.
HTH
Stephen

kriskross wrote:

> Hi  there.
> I have a problem with a entity that I can't figure out. Hope someone can
> helpe me out.
>
> /**** Class****/
> @Entity
> public class A implements Serializable {
>     @Id
>     @GeneratedValue(strategy =
> GenerationType.SEQUENCE,generator="A_aId_seq")
>     private Integer aId;
>     private String name;
> }
> /***********method addet to AFacadeRemote**********/
> private A uppdateMe(A a) {
>    return em.merge(a);
> }
>
>
> /********* method ********/
> public Integer getAId(String test) {
>    AFacadeRemote afr;
>    A a = new A(test);
>    afr.create(a);
>    a = afr.updateMe(a);
>    return a.getAId();
> }
>
> In my db I now have a record.
> But the method returns null;
> Any idea how I can create A and return A.getAId() in the same method. It is
> not possible to create a finder method and search for A.name, name is not
> unique.
>
> Best regards
> Kristian
>  
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @EJB @GeneratedValue(strategy = GenerationType.SEQUENCE

kriskross
I'm using PostgreSQL.
I'm testing nativeSQL now, testing with a method, but this gives me two problems.
1. em.createNativeQuery creates an error.
2. This should probably be executed in a transaction. If two users creates a new A at the same time SELECT currval will probably not return the correct new id.


public A createAndReturn(A a) {
        em.persist(a);
        Integer id = (Integer)em.createNativeQuery("SELECT currval('A_aId_seq')", Integer.class).getSingleResult();
        a.setAId(id);
        return a;
    }

Any tips?

Stephen Kaspersen wrote
You could drop into native SQL and search for the last_insert_id()  for
that table.

Are you using Mysql?
I have found using an autoincrement column and GenerationType.IDENTITY
does update the Entity when its created.
HTH
Stephen
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @EJB @GeneratedValue(strategy = GenerationType.SEQUENCE

Stephen Kaspersen
Have you tried with GenerationType.IDENTITY

if your field declaration is "aId int DEFAULT nextval('A_aId_seq') " I think it might work.

Stephen

kriskross wrote:
I'm using PostgreSQL. 
I'm testing nativeSQL now, testing with a method, but this gives me two
problems.
1. em.createNativeQuery creates an error.
2. This should probably be executed in a transaction. If two users creates a
new A at the same time SELECT currval will probably not return the correct
new id.


public A createAndReturn(A a) {
        em.persist(a);
        Integer id = (Integer)em.createNativeQuery("SELECT
currval('A_aId_seq')", Integer.class).getSingleResult();
        a.setAId(id);
        return a;
    }

Any tips?


Stephen Kaspersen wrote:
  
You could drop into native SQL and search for the last_insert_id()  for 
that table.

Are you using Mysql?
I have found using an autoincrement column and GenerationType.IDENTITY 
does update the Entity when its created.
HTH
Stephen
  

    

  
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @EJB @GeneratedValue(strategy = GenerationType.SEQUENCE

kriskross
Changing to
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator="a_id_seq")
    private int id;

is still the same. When trying to get a.getId() it's now 0

Stephen Kaspersen wrote


 


Have you tried with GenerationType.IDENTITY

if your field declaration is "aId int DEFAULT nextval('A_aId_seq')
" I think it might
work.

Stephen

kriskross wrote:

  I'm using PostgreSQL.
I'm testing nativeSQL now, testing with a method, but this gives me two
problems.
1. em.createNativeQuery creates an error.
2. This should probably be executed in a transaction. If two users creates a
new A at the same time SELECT currval will probably not return the correct
new id.


public A createAndReturn(A a) {
        em.persist(a);
        Integer id = (Integer)em.createNativeQuery("SELECT
currval('A_aId_seq')", Integer.class).getSingleResult();
        a.setAId(id);
        return a;
    }

Any tips?


Stephen Kaspersen wrote:
 
 
    You could drop into native SQL and search for the last_insert_id()  for
that table.

Are you using Mysql?
I have found using an autoincrement column and GenerationType.IDENTITY
does update the Entity when its created.
HTH
Stephen
 

   
 
 
 


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: @EJB @GeneratedValue(strategy = GenerationType.SEQUENCE

kriskross
If anyone else is having trouble with the same, i fixed this with:
public A create(A a) {
em.persist(a);
em.flush();
return a
}

kriskross wrote
Changing to
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator="a_id_seq")
    private int id;

is still the same. When trying to get a.getId() it's now 0

Stephen Kaspersen wrote


 


Have you tried with GenerationType.IDENTITY

if your field declaration is "aId int DEFAULT nextval('A_aId_seq')
" I think it might
work.

Stephen

kriskross wrote:

  I'm using PostgreSQL.
I'm testing nativeSQL now, testing with a method, but this gives me two
problems.
1. em.createNativeQuery creates an error.
2. This should probably be executed in a transaction. If two users creates a
new A at the same time SELECT currval will probably not return the correct
new id.


public A createAndReturn(A a) {
        em.persist(a);
        Integer id = (Integer)em.createNativeQuery("SELECT
currval('A_aId_seq')", Integer.class).getSingleResult();
        a.setAId(id);
        return a;
    }

Any tips?


Stephen Kaspersen wrote:
 
 
    You could drop into native SQL and search for the last_insert_id()  for
that table.

Are you using Mysql?
I have found using an autoincrement column and GenerationType.IDENTITY
does update the Entity when its created.
HTH
Stephen
 

   
 
 
 


Loading...