Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Toplink JPA insertable problem

 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm pretty new to JavaEE technology. Hopefully, you guys can help me out with this issue that I'm facing.

I have a table created, which inside it there's a column "id" which is a primary key field and the key will be auto generated by default. On the other hand. I've a Entity class, where the persistence provider will auto include every field to database. My question is, how can I prevent this from happening?

By the way, I've tried using @Column(insertable=false). This doesn't seems working though.

Thanks.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ming Ming.

Have you played around with the @GeneratedValue annotation?

https://glassfish.dev.java.net/nonav/javaee5/api/javax/persistence/GeneratedValue.html



-Cameron McKenzie
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I've tried that, but it doesn't seem to work.
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help needed very much guys Cheers.
 
Gordon Yorke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the ID generated? If it is generated by the database column (ie an identity field) then use:
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realised everytime when I use @GeneratedValue(strategy=IDENTITY), there will be an error in the .class file. It will give me something like this @GeneratedValue(strategy="<error>")

Wonder why? Is it something wrong with my netbeans compiler?
 
Gordon Yorke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using a static import for the enum? perhaps try
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same error on the class file as well. Do I really need the @GeneratedValue(strategy=GenerationType.IDENTITY) . Since in this case, my database will automatically generate the value for me, instead of the persistence provider?

I read from the Java API docs, it says that if we have @Column(insertable=false) , the persistence provider will ignore this column for the insert statement. But for me it is not happening, every time it still insert to the column, which at the end will throw error, because of having the same primary key.
 
Gordon Yorke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The PK must be writable or use IDENTITY sequencing. What is the error you are seeing.
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This exception thrown is Follow by

This is the way, I created the table



Therefore, that's why I don't need the persistence provider to insert that column for me. Any idea?
 
Gordon Yorke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The PK must be writable or use IDENTITY sequencing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic