facebook

[Closed] jpa reverse engine exclude PK when save

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #272899 Reply

    feili
    Member

    Hi,

    When I run JPA, I can do everything except save. If I save I get error:

    An explicit value for the identity column in table ‘Country’ can only be specified when a column list is used and IDENTITY_INSERT is ON

    The error is correct because our database table’s PK is databse automatically generated. When I use insert sql command I have to drop PK value. Otherwise I will get the above error.

    How should I do?

    This problem is fatal problem to me.

    Thanks

    #272909 Reply

    Riyad Kalla
    Member

    feili,
    Do you have an example table DDL you can post, along with the generated JPA entity class so we can look at some specifics?

    Also what DB are you using?

    #272940 Reply

    feili
    Member

    Hi,

    This is a concept issue. I copy some text from Internet for you.

    Primary Key Provided by Database on INSERT
    Sometimes databases have their own proprietary mechanism to generate a primary key when a new row is inserted. This feature is called “auto increment” or “identity column”. Currently MySQL and SQLServer have this feature turned on by default

    See the problem? For such database table (any table, does not matter) you cannot insert a record with a primary key. My question is:
    1. JPA can handle such table?
    2. How to do that

    This is not a specific database or table issue. You do not need to look at any code or table.

    Thanks

    Fei LI

    #272944 Reply

    feili
    Member

    I have found the solution:

    After line

    @Id

    add a new line
    @GeneratedValue(strategy=GenerationType.IDENTITY)

    Thanks

    #272967 Reply

    Loyal Water
    Member

    Thanks for letting us know.

    #272973 Reply

    Riyad Kalla
    Member

    Feili,
    Sorry for the brain-fart on my part, I should have recognized what you are asking. Yes JPA handles that just fine, when you are storing/retrieving the entities, just be sure to set no ID value, and after you persist the entity, JPA will popular the ID field for you.

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: [Closed] jpa reverse engine exclude PK when save

You must be logged in to post in the forum log in