|
Replies:
7
-
Last Post:
Jun 6, 2006 7:05 PM
by: pramodgo
|
|
|
|
|
|
|
EJB3 @generatedvalue not working
Posted:
May 31, 2006 6:58 AM
|
|
|
Hi,
The annotation is not working, i manually added 2 users to my table. As soon as i call the EJB3, i get an error. Here is relevant error and code.
Any idea what i am doing wrong? code below error.
If you need more code, i will post. Thanks in advance.
-----------Error-------------------------
|#] EJB5071: Some remote or transactional roll back exception occurred Local Exception Stack: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.4 (Build 060412)): oracle.toplink.essentials.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 1Error Code: 1062 Call:INSERT INTO client (CNo, CName, CUserName, CAddr1, CAddr2, CAddr3, CEmail, CPassword, CTitle, CTelNo, CCreditCard, CSName) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [1, null, null, null, null, null, null, null, null, 0, 0, null] Query:InsertObjectQuery(1) at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:295) at oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:639) at oracle.toplink.essentials.internal.databasea
---------------Relevant code-----------------------
---------------Struts Action----------------------
RegisterActionForm registerform = new RegisterActionForm(); Client client = new Client(); // using the BeanUtils class to copy data from form to object //client BeanUtils.copyProperties(client,registerform); lookupClientFacade().create(client);
-----------------Entity bean------------------------
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "CNo", nullable = false) private Integer cNo;
|
|
|
|
|
|
|
Re: EJB3 @generatedvalue not working
Posted:
May 31, 2006 10:20 AM
in response to: infonote
|
|
|
Hello,
Actually, it looks as though @generatedvalue is working. The problem is that you created 2 entries yourself (likely using pk values 1 and 2?) but did not use or update the sequence value in the sequencing mechanism. The value in the mechanism needs to be incremented if you are going to manually insert objects, much the same way you would use a sequencing object.
If you are using auto, I believe this is the same as using table sequencing. So you will need to find the table and row containing the sequencing value for your table and increment it for each insert. If you set the toplink.logging.level property to fine, you should see the statements TopLink uses to manage the sequence values.
Best Regards, Chris
|
|
|
|
|
|
|
|
Re: EJB3 @generatedvalue not working
Posted:
May 31, 2006 12:49 PM
in response to: chris_delahunt
|
|
|
Yes, i manually added the data to check if my login works.
Can you further elaborate on the sequencing thing. Is it a change in the DB or in the EJB? i.e. i have to put something like this.
In Database i cannot change much, since i already have ticked AutoIncrement in MySql.
So it is an EJB thing, what annotation should i use? The sequencegenerator?
Thanks a lot for the response.
http://www.hibernate.org/hib_docs/ejb3-api/javax/persistence/SequenceGenerator.html
|
|
|
|
|
|
|
|
Re: EJB3 @generatedvalue not working
Posted:
May 31, 2006 1:24 PM
in response to: infonote
|
|
|
Or using @TableGenerator as in the JEE5 tutorial? What i really need is an example of what you meant.
Thanks a lot
|
|
|
|
|
|
|
|
Re: EJB3 @generatedvalue not working
Posted:
May 31, 2006 7:44 PM
in response to: infonote
|
|
|
You shouldn't need to use the @TableGenerator annotation. What chris is trying to point out is that in all likely hood you _actually_ have duplicate primary keys. Since you manually inserted the record with primary key 1, the persistence provider has no way of knowing that '1' as a primary key has already been used. One solution is, as chris pointed out, that you find the table being used to store the primary key and manually update the sequence to reflect that the primary keys, that you manually inserted, have been used.
e.g. Assume you have the table_A for entity_A and you have inserted the following values manually
pk name --- ---- 1 xyz 2 abc
Also assume the provider is using a table for generating the primary keys, which is most likely the case here, and the table is sequence
seq_name seq_count ---------- --------- seq_gen 0
now since you have manually inserted the data in table_A when the provider gets the next primary key, it gets 1, because it has no way of knowing that you manually inserted data and used up primary keys 1 and 2.
You will still get this exception even if you used @TableGenerator and followed the same process of manually inserting the data.
hth.
|
|
|
|
|
|
|
|
Re: EJB3 @generatedvalue not working
Posted:
Jun 5, 2006 5:01 PM
in response to: infonote
|
|
|
Hi all; I have a problem with the @GeneratedValue annotation. When I'm writing an EJB Class and put this annotation after @id; I have an error message telling me that "@GeneratedValue cannot be resolved to a type".
I'm waiting for your suggestions Thanks
|
|
|
|
|
|
|
|
Re: EJB3 @generatedvalue not working
Posted:
Jun 5, 2006 5:11 PM
in response to: tamera
|
|
|
When is this error message generated?
I am guessing you meant compile time error, In that case make sure you have imported
javax.persistence.GeneratedValue
|
|
|
|
|
|
|
|
Re: EJB3 @generatedvalue not working
Posted:
Jun 6, 2006 7:05 PM
in response to: infonote
|
|
|
Hi Since you are using the Identity fields of Mysql, you would need to change your entity code to be : ---------------------- @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "CNo", nullable = false) private Integer cNo; ----------------------
When you defin GenerationType.AUTO, you are letting the persistence provider to pick up an appropriate strategy. But in your case since you have the entity pointing to a field in mysql that is of type Identity, it is recommened that you define "GenerationType.IDENTITY".
Additionally there is a difference in behavior of identity fields, as the value of the id field would be obtained after the record has been inserted into the database.
|
|
|
|
|