-
1. Re: How to get generated id after persiting an entity?
jaikiran Oct 23, 2008 3:21 AM (in response to mike_ap)That's strange. Are you sure you are calling the getId on the correct (returned) object? Can you post that part of the code where you are using this returned object? Also what does myEntity.getId() print, just before returning from the bean method?
-
2. Re: How to get generated id after persiting an entity?
mike_ap Oct 23, 2008 9:39 AM (in response to mike_ap)Here's my problem (in detail):
-The console app returns 0 from the getId() method.
-The session bean returns 20051 from the getId() method.
- And the oracle database stores 322 as the Id
What am I doing wrong?
I have a test console app that does this:context = new InitialContext(); LogFileManagerRemote beanRemote = (LogFileManagerRemote) context.lookup(LogFileManager.RemoteJNDIName); MyLogFile file = new MyLogFile("newfile.log", 12345); beanRemote.saveMyLogFile(file); System.out.println(file.getId());
my entity looks like this:mport java.sql.Date; import javax.persistence.*; import java.io.Serializable; @SuppressWarnings("serial") @Entity @Table(name="LOG_FILE") @SequenceGenerator(name="LOG_FILE_SEQUENCE_GENERATOR", sequenceName="LOG_FILE_SEQ") public class MyLogFile implements Serializable{ /** * */ private long id; private String fullFileName; private Date createdTimestamp; private long lineCount; public MyLogFile(){ } public MyLogFile(String fileName){ this.fullFileName = fileName; } public MyLogFile(String fileName, long lineCount){ this.fullFileName = fileName; this.lineCount = lineCount; } @Id() @Column(name="FILE_ID") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "LOG_FILE_SEQUENCE_GENERATOR") public long getId() { return id; } protected void setId(long id) { this.id = id; } @Column(name="FULL_FILE_NAME") public String getFullFileName() { return fullFileName; } public void setFullFileName(String fullFileName) { this.fullFileName = fullFileName; } @Column(name="CREATED") public Date getCreatedTimestamp() { return createdTimestamp; } protected void setCreatedTimestamp(Date createdTimestamp) { this.createdTimestamp = createdTimestamp; } @Column(name="LINE_COUNT") public long getLineCount() { return lineCount; } public void setLineCount(long lineCount) { this.lineCount = lineCount; } @Override public String toString() { // TODO Auto-generated method stub return super.toString(); } }
and my session bean has this:import javax.ejb.Stateless; import javax.persistence.*; import local.mytestpackage.MyLogFile; @Stateless public class LogFileManager implements LogFileManagerLocal, LogFileManagerRemote { @PersistenceContext EntityManager em; public static final String RemoteJNDIName = LogFileManager.class.getSimpleName() + "/remote"; public static final String LocalJNDIName = LogFileManager.class.getSimpleName() + "/local"; public MyLogFile saveMyLogFile(MyLogFile file){ em.persist(file); em.flush(); return file; } }
-
3. Re: How to get generated id after persiting an entity?
jaikiran Oct 23, 2008 9:47 AM (in response to mike_ap)"mike_ap" wrote:
context = new InitialContext(); LogFileManagerRemote beanRemote = (LogFileManagerRemote) context.lookup(LogFileManager.RemoteJNDIName); MyLogFile file = new MyLogFile("newfile.log", 12345); beanRemote.saveMyLogFile(file); System.out.println(file.getId());
When you are using a remote bean, the parameters are passed by value. So any changes to the "file" entity that you are passing will not be reflected in the method that calls the bean. So at your client, you should use the object returned by your bean. That object will have the id set:MyLogFile savedFile = beanRemote.saveMyLogFile(file); System.out.println(savedFile.getId());
-
4. Re: How to get generated id after persiting an entity?
mike_ap Oct 23, 2008 9:53 AM (in response to mike_ap)Thank you for explaining that. That solved one issue. I now get the same value in the session bean and from the console app.
But why would I still get a different value stored in the database? -
5. Re: How to get generated id after persiting an entity?
jaikiran Oct 23, 2008 10:11 AM (in response to mike_ap)Are you sure you are looking at the right row and column in the database table?
-
6. Re: How to get generated id after persiting an entity?
mike_ap Oct 23, 2008 10:21 AM (in response to mike_ap)I deleted all the records from the table so I know I am looking at the correct row and column.
Eureka! I know what the problem is!
I have a trigger in the database that creates a new Id on each record.
I dropped the trigger and the Id's are in sync now!