facebook

Scaffolding auto_increment for MySQL not generating @GeneratedValue

  1. MyEclipse IDE
  2.  > 
  3. Spring Development
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #391592 Reply

    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

    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

    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 ?

    2016-04-05 16:07:48,393 ERROR [localhost-startStop-1] ContextLoader - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'conEhealthdb' defined in class path resource [eHealth_Backend-dao-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: conEhealthdb] class or package not found
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
    	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)
    	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1897)
    	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    	at java.lang.Thread.run(Thread.java:745)
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: conEhealthdb] class or package not found
    	at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1401)
    	at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1184)
    	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1048)
    	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:693)
    	at org.springframework.orm.jpa.vendor.SpringHibernateEjbPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateEjbPersistenceProvider.java:50)
    	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:341)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
    	... 25 more
    Caused by: java.lang.ClassNotFoundException: ehealth.domain.Kid
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:348)
    	at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:170)
    	at org.hibernate.ejb.Ejb3Configuration.classForName(Ejb3Configuration.java:1318)
    	at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1390)
    	... 33 more
    #475228 Reply

    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.

    Referenced file contains errors (project-aware://eHealth_Backend/org/springframework/beans/factory/xml/spring-beans-4.1.xsd).  For more information, right click on the message in the Problems View and select "Show Details..."	jaxws-web-context.xml	/eHealth_Backend/src/jaxws	line 1	XML Problem
    

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

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:jaxws="http://cxf.apache.org/jaxws"
    	xmlns:cxf="http://cxf.apache.org/core"
    	xmlns:http="http://cxf.apache.org/transports/http/configuration"
    		xsi:schemaLocation="http://www.springframework.org/schema/beans 	
    		http://www.springframework.org/schema/beans/spring-beans-4.1.xsd	
    		http://www.springframework.org/schema/context 	
    		http://www.springframework.org/schema/context/spring-context-4.1.xsd	
    		http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    		http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
    		http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">
    	<import resource="classpath:META-INF/cxf/cxf.xml" />
    	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    	
    	
    
        <bean id="org.skyway.spring.util.webservice.cxf.WSSecurityOutInterceptor" class="org.skyway.spring.util.webservice.cxf.WSSecurityOutInterceptor"/>
    
        <cxf:bus>
            <cxf:outInterceptors>
                <ref bean="org.skyway.spring.util.webservice.cxf.WSSecurityOutInterceptor"/>
            </cxf:outInterceptors>
            <!-- Uncomment to enable logging of incoming/outgoing requests-->
    		<cxf:features > 
    			<cxf:logging /> 
    		</cxf:features>
    		
        </cxf:bus>
    
      	<http:conduit name="*.http-conduit">
      		<http:authSupplier class="org.skyway.spring.util.webservice.cxf.HttpBasicAuthenticationSupplier" />
      	</http:conduit>
    	
    
    </beans>
    #475239 Reply

    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

    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

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