[jboss-user] [EJB 3.0] - Re: How to get generated id after persiting an entity?

mike_ap do-not-reply at jboss.com
Thu Oct 23 09:39:32 EDT 2008


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;
  | 	}
  | 
  | 
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4184206#4184206

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4184206



More information about the jboss-user mailing list