File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Toplink JPA insertable problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Toplink JPA insertable problem" Watch "Toplink JPA insertable problem" New topic
Author

Toplink JPA insertable problem

ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: May 17, 2006
Posts: 54
Yeah, I've tried that, but it doesn't seem to work.
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
Help needed very much guys Cheers.
Gordon Yorke
Greenhorn

Joined: Nov 16, 2009
Posts: 7
How is the ID generated? If it is generated by the database column (ie an identity field) then use:
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
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

Joined: Nov 16, 2009
Posts: 7
Are you using a static import for the enum? perhaps try
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
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

Joined: Nov 16, 2009
Posts: 7
The PK must be writable or use IDENTITY sequencing. What is the error you are seeing.
ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
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

Joined: Nov 16, 2009
Posts: 7
The PK must be writable or use IDENTITY sequencing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Toplink JPA insertable problem