This content has been marked as final.
Show 8 replies
-
1. Re: getting the id of the object inserted...
dhinojosa Oct 20, 2007 11:13 PM (in response to madhav_inamti)Pojo pojo = new Pojo();
//Fill pojo with info....
entityManager.persist(pojo);
pojo.getId(); //There it is.... -
2. Re: getting the id of the object inserted...
madhav_inamti Oct 21, 2007 10:17 PM (in response to madhav_inamti)This does not work. The id is not in the object after a getId() call.
-
3. Re: getting the id of the object inserted...
modoc Oct 22, 2007 2:48 AM (in response to madhav_inamti)I'm pretty sure it should:) Can you post your object code?
-
4. Re: getting the id of the object inserted...
david.spark Oct 22, 2007 4:03 AM (in response to madhav_inamti)If you use
entityManager.merge(pojo)
then it returns the persisted Pojo object from which you can get the ID.
So, for example, you can doPojo pojo = new Pojo(); //Fill pojo with info.... Pojo pojo2 = entityManager.merge(pojo); pojo2.getId(); //There it is....
Actual code might need to be more robust :-) -
5. Re: getting the id of the object inserted...
nickarls Oct 22, 2007 5:50 AM (in response to madhav_inamti)pojo = entityManager.merge(pojo)
might also work
perhaps alsoentityManager.refresh(pojo)
depending on the state of the pojo -
6. Re: getting the id of the object inserted...
dustismo Oct 22, 2007 12:13 PM (in response to madhav_inamti)Make sure you use a GeneratedValue annotation in your model.
I.e.@Id @Column(name = "id", unique = true, nullable = false) @GeneratedValue(strategy = GenerationType.IDENTITY) @NotNull public long getId() { return this.id; }
-
7. Re: getting the id of the object inserted...
madhav_inamti Oct 22, 2007 12:38 PM (in response to madhav_inamti)That was it. Having a @GeneratedValue makes the getId() work. Looks like Seam gen could infer the AUTO_INCREMENT in Mysql and codegen the @GeneratedValue.
Thanks,
M -
8. Re: getting the id of the object inserted...
modoc Oct 22, 2007 1:32 PM (in response to madhav_inamti)Actually @GeneratedValue and AUTO_INCREMENT are two different ways of setting up your IDs. I don't think I'd want seam-gen marking columns with GeneratedValue just based on an auto_increment in the database.
However I'm glad you found the solution to your problem!