0

There is a problem about generating id while persisting into database. I added the following code to my jpa entity file, however I'm getting 0 for personid.

@Id
@Column(unique=true, nullable=false, precision=10, name="PERSONID")
@SequenceGenerator(name="appUsersSeq", sequenceName="SEQ_PERSON", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "appUsersSeq")
private long personid;

EjbService:

@Stateless
public class EjbService implements EjbServiceRemote {

@PersistenceContext(name = "Project1245")
private EntityManager em;


@Override
public void addTperson(Tperson tp) {
    em.persist(tp);

}

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
coderxyz24
  • 11
  • 2
  • 11

1 Answers1

0

0 is default value for long type. The id will be set after invoking select query for the related sequence, which commonly is executed when you persist the entity. Are you persisting the entity? In case yes, post the database sequence definition to check it.

Guillermo
  • 1,523
  • 9
  • 19
  • `CREATE SEQUENCE "DB"."SEQ_PERSON" MINVALUE 1 MAXVALUE 999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE ;` – coderxyz24 Aug 20 '15 at 09:10
  • The records in your database do have *peraonid* value 0? Are you checking the value of the java object id after `persist` the entity? – Guillermo Aug 20 '15 at 09:22
  • No there is no 0 value personid in my database – coderxyz24 Aug 20 '15 at 09:23
  • The sequence looks fine. As I said, when you create a `new` java object the personid value is 0. The value will be replaced with the given sequence value after you *persist* the object, I mean when you pass it to `EntityManager.persist` or the object is persisted because of a cascade persist propagation or some other reason. – Guillermo Aug 20 '15 at 09:31
  • there is no record with an id 0 in database and it never changes, always getting 0 although I put sequencegenerator – coderxyz24 Aug 20 '15 at 10:31
  • So is it inserting the records with right id value in the database? – Guillermo Aug 20 '15 at 13:59
  • No It doesn't insert anything – coderxyz24 Aug 20 '15 at 14:14
  • But are you trying to persist it? Edit the question adding your logic of persistence. – Guillermo Aug 20 '15 at 15:36
  • Your code looks fine so far. Add the code that use `EjbService` to persist the entity and specify at what point in the code you evaluate and see 0 value for personid. `flush` call and transaction manipulation (if any) is important to understand what you are doing – Guillermo Aug 22 '15 at 20:03