Scaffolding auto_increment for MySQL not generating @GeneratedValue

This topic contains 9 replies, has 3 voices, and was last updated by Profile photo of support-swapna support-swapna 1 year, 7 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #391592 Reply
    Profile photo of SwingCarlos
    SwingCarlos
    Participant

    I have several tables with the primary key set to AUTO_INCREMENT. Like this example:
    CREATE TABLE password_question (
    password_question_id INT NOT NULL AUTO_INCREMENT,
    question VARCHAR(256) NOT NULL,
    PRIMARY KEY(password_question_id),
    created_date TIMESTAMP DEFAULT NOW(),
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    When I try to use the save using the generated post URL and the following json payload:
    {
    “createdDate”: 1432443205000,
    “question”: “Who killed JR?”
    }

    threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): mydroneracedb.domain.PasswordQuestion] with root cause
    org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): mydroneracedb.domain.PasswordQuestion

    Now I know this is caused by a missing @GeneratedValue, which scaffolding has not created on my primary keys. I had to add it in manually like below:
    @Column(name = “password_question_id”, nullable = false)
    @basic(fetch = FetchType.EAGER)
    @id
    @GeneratedValue(strategy=GenerationType.IDENTITY) // Had to add this manually.
    @XmlElement
    Integer passwordQuestionId;

    When is this going to be fixed? I saw a similar thread from 2011 where the workaround was outlined, but no confirmation that this was ever worked on.

    Carlos

    #391618 Reply
    Profile photo of support-swapna
    support-swapna
    Moderator

    Carlos,

    We have a bug filed for this issue but unfortunately not fixed due to other higher priority issues.The dev team is looking into it. We will keep you posted about the timeline for the fix.

    Sorry for inconvenience caused.

    –Swapna
    MyEclipse Support

    #474887 Reply

    Hi Carlos,

    I’m having the same problem but even if I try to add @GeneratedValue(strategy=GenerationType.IDENTITY), It’s still not work. Could you please give me some ideas about this ? 🙁

    #474961 Reply
    Profile photo of support-swapna
    support-swapna
    Moderator

    Hi,

    Sorry that you are seeing this issue.

    1. Can you please elaborate on what exact issue you are seeing even after manually adding the @GeneratedValue ?
    2. Please share with us the MyEclipse installation details from menu MyEclipse > Installation Summary > Installation Details along with the MySQL Create Table code to help us replicate the issue at our end.
    3. Please also share with us the .log file located at /.metadata/.log for further investigation.

    –Swapna
    MyEclipse Support

    #475088 Reply

    Thank for your reply,
    1. Sorry, I just realize that I need to set ID = 0 whenever I insert object to database.
    2. Now I just install new version of Myeclipse 2016 CL
    But I have another problem when I clean project and run on server again :
    Could you please help me with this problem ?

    #475228 Reply
    Profile photo of support-swapna
    support-swapna
    Moderator

    Hi,

    1. Can you please check if the path of classes is correct in your persistence.xml ?
    2. Please also check if all the Hibernate dependencies in the project are correct and there are no extra dependencies.

    If the issue persists, please share with us the project to help us investigate at our end.

    Let us know how it works.

    –Swapna
    MyEclipse Support

    #475230 Reply

    Hi,
    1. I check persistence.xml path. It’s correct. It’s generated when I do scaffolding.
    Sorry, This problem happens when I do Maven Clean (Not Project clean).
    2. And Here’s my Hibernate dependencies
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.1.4.Final</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate.common</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>4.0.1.Final</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
    </dependency>
    <dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.15.0-GA</version>
    </dependency>
    <dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <version>3.1.0.GA</version>
    </dependency>
    <dependency>
    <groupId>org.jboss.spec.javax.transaction</groupId>
    <artifactId>jboss-transaction-api_1.1_spec</artifactId>
    <version>1.0.0.Final</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.1.4.Final</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.2.0.Final</version>
    </dependency>

    3. I’m using MyEclipse on Mac OS. And I got another error. I already tried to find solution on the internet but those can’t solve my problem. Could you please have a look.

    And this is my jaxws-web-context.xml file :

    #475239 Reply
    Profile photo of support-swapna
    support-swapna
    Moderator

    Hi,

    Thank you for the details.

    java.lang.ClassNotFoundException: ehealth.domain.Kid

    It looks like the ehealth.domain.Kid class is not on the project’s classpath and since its declared in the persistence.xml you are seeing the error. Can you please recheck how you are adding those dependencies?

    Please send us your project to help us investigate better. You can email us the zipped project to swapna@genuitec.com with a reference to this thread.

    –Swapna
    MyEclipse Support

    #475338 Reply

    Hi,

    After I tried to clean project again and again. Now this error is disappear.
    ehalth.domain.Kid is in the project’s path because it’s generated when i do scaffolding.
    Anyway, Now error is gone. Thank.

    I’ve other question, Sorry because i’m new with this.
    Could you please tell me How to get return generated value ID ?
    After save object in to DB by server

    country = countryDAO.store(country);

    I don’t know how get the return ID.
    Please give me explanation.
    Thank

    #475340 Reply
    Profile photo of support-swapna
    support-swapna
    Moderator

    Hi,

    Please check these threads for suggestions about getting the return ID after save/insert :

    http://stackoverflow.com/questions/25561681/hibernate-get-id-after-save-object
    http://forum.spring.io/forum/spring-projects/data/3849-hibernate-return-value-after-insert
    http://stackoverflow.com/questions/7442756/hibernate-return-auto-generate-id-of-a-newly-inserted-row

    I suggest you also check on development related forums for better support.

    Hope this helps. Let us know if you see any issues.

    –Swapna
    MyEclipse Support

Viewing 10 posts - 1 through 10 (of 10 total)
Reply To: Scaffolding auto_increment for MySQL not generating @GeneratedValue