mysql - Duplicate entry '1' for key 'UK_tbce3olw19taxx6srova86xok' -


good day people.

i'm trying figure out how deal hibernate. in test use hibernate inheritance , exception i've got:

hibernate: select theuser0_.id id1_2_0_, theuser0_.first_name first_na3_2_0_, theuser0_.last_name last_nam4_2_0_, theuser0_.password password5_2_0_, theuser0_.emale emale6_2_0_, theuser0_.gender gender7_2_0_, theuser0_.uci_person_address uci_pers8_2_0_, theuser0_.uci_person_phone uci_pers9_2_0_, theuser0_.user_info_type user_inf2_2_0_ user theuser0_ theuser0_.id=? hibernate: insert user_events (event_date, user_id, event_index) values (?, ?, ?) oct 07, 2013 2:51:53 pm org.hibernate.engine.jdbc.spi.sqlexceptionhelper logexceptions warn: sql error: 1062, sqlstate: 23000 oct 07, 2013 2:51:53 pm org.hibernate.engine.jdbc.spi.sqlexceptionhelper logexceptions error: duplicate entry '1' key 'uk_tbce3olw19taxx6srova86xok' exception in thread "main" org.springframework.dao.dataintegrityviolationexception: not execute statement; sql [n/a]; constraint [null]; nested exception org.hibernate.exception.constraintviolationexception: not execute statement     @ org.springframework.orm.hibernate4.sessionfactoryutils.converthibernateaccessexception(sessionfactoryutils.java:138)     @ org.springframework.orm.hibernate4.hibernatetransactionmanager.converthibernateaccessexception(hibernatetransactionmanager.java:680)     @ org.springframework.orm.hibernate4.hibernatetransactionmanager.docommit(hibernatetransactionmanager.java:562)     @ org.springframework.transaction.support.abstractplatformtransactionmanager.processcommit(abstractplatformtransactionmanager.java:755)     @ org.springframework.transaction.support.abstractplatformtransactionmanager.commit(abstractplatformtransactionmanager.java:724)     @ org.springframework.transaction.interceptor.transactionaspectsupport.committransactionafterreturning(transactionaspectsupport.java:475)     @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:270)     @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:94)     @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172)     @ org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:204)     @ com.sun.proxy.$proxy7.saveuser(unknown source)     @ demidov.pkg.persistence.appout.main(appout.java:36) caused by: org.hibernate.exception.constraintviolationexception: not execute statement     @ org.hibernate.exception.internal.sqlexceptiontypedelegate.convert(sqlexceptiontypedelegate.java:74)     @ org.hibernate.exception.internal.standardsqlexceptionconverter.convert(standardsqlexceptionconverter.java:49)     @ org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:125)     @ org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:110)     @ org.hibernate.engine.jdbc.internal.resultsetreturnimpl.executeupdate(resultsetreturnimpl.java:136)     @ org.hibernate.id.identitygenerator$getgeneratedkeysdelegate.executeandextract(identitygenerator.java:96)     @ org.hibernate.id.insert.abstractreturningdelegate.performinsert(abstractreturningdelegate.java:58)     @ org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:2975)     @ org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:3487)     @ org.hibernate.action.internal.entityidentityinsertaction.execute(entityidentityinsertaction.java:81)     @ org.hibernate.engine.spi.actionqueue.execute(actionqueue.java:377)     @ org.hibernate.engine.spi.actionqueue.addresolvedentityinsertaction(actionqueue.java:214)     @ org.hibernate.engine.spi.actionqueue.addinsertaction(actionqueue.java:194)     @ org.hibernate.engine.spi.actionqueue.addaction(actionqueue.java:178)     @ org.hibernate.event.internal.abstractsaveeventlistener.addinsertaction(abstractsaveeventlistener.java:321)     @ org.hibernate.event.internal.abstractsaveeventlistener.performsaveorreplicate(abstractsaveeventlistener.java:286)     @ org.hibernate.event.internal.abstractsaveeventlistener.performsave(abstractsaveeventlistener.java:192)     @ org.hibernate.event.internal.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:125)     @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid(defaultsaveorupdateeventlistener.java:206)     @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.entityistransient(defaultsaveorupdateeventlistener.java:191)     @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.performsaveorupdate(defaultsaveorupdateeventlistener.java:114)     @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:90)     @ org.hibernate.internal.sessionimpl.firesaveorupdate(sessionimpl.java:735)     @ org.hibernate.internal.sessionimpl.saveorupdate(sessionimpl.java:727)     @ org.hibernate.engine.spi.cascadingaction$5.cascade(cascadingaction.java:258)     @ org.hibernate.engine.internal.cascade.cascadetoone(cascade.java:388)     @ org.hibernate.engine.internal.cascade.cascadeassociation(cascade.java:331)     @ org.hibernate.engine.internal.cascade.cascadeproperty(cascade.java:209)     @ org.hibernate.engine.internal.cascade.cascadecollectionelements(cascade.java:418)     @ org.hibernate.engine.internal.cascade.cascadecollection(cascade.java:358)     @ org.hibernate.engine.internal.cascade.cascadeassociation(cascade.java:334)     @ org.hibernate.engine.internal.cascade.cascadeproperty(cascade.java:209)     @ org.hibernate.engine.internal.cascade.cascade(cascade.java:166)     @ org.hibernate.event.internal.abstractflushingeventlistener.cascadeonflush(abstractflushingeventlistener.java:162)     @ org.hibernate.event.internal.abstractflushingeventlistener.prepareentityflushes(abstractflushingeventlistener.java:153)     @ org.hibernate.event.internal.abstractflushingeventlistener.flusheverythingtoexecutions(abstractflushingeventlistener.java:89)     @ org.hibernate.event.internal.defaultflusheventlistener.onflush(defaultflusheventlistener.java:51)     @ org.hibernate.internal.sessionimpl.flush(sessionimpl.java:1234)     @ org.hibernate.internal.sessionimpl.managedflush(sessionimpl.java:404)     @ org.hibernate.engine.transaction.internal.jdbc.jdbctransaction.beforetransactioncommit(jdbctransaction.java:101)     @ org.hibernate.engine.transaction.spi.abstracttransactionimpl.commit(abstracttransactionimpl.java:175)     @ org.springframework.orm.hibernate4.hibernatetransactionmanager.docommit(hibernatetransactionmanager.java:554)     ... 9 more caused by: com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception: duplicate entry '1' key 'uk_tbce3olw19taxx6srova86xok'     @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method)     @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:57)     @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45)     @ java.lang.reflect.constructor.newinstance(constructor.java:526)     @ com.mysql.jdbc.util.handlenewinstance(util.java:411)     @ com.mysql.jdbc.util.getinstance(util.java:386)     @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:1041)     @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4190)     @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4122)     @ com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:2570)     @ com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:2731)     @ com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2818)     @ com.mysql.jdbc.preparedstatement.executeinternal(preparedstatement.java:2157)     @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2460)     @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2377)     @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2361)     @ org.apache.commons.dbcp.delegatingpreparedstatement.executeupdate(delegatingpreparedstatement.java:105)     @ org.apache.commons.dbcp.delegatingpreparedstatement.executeupdate(delegatingpreparedstatement.java:105)     @ org.hibernate.engine.jdbc.internal.resultsetreturnimpl.executeupdate(resultsetreturnimpl.java:133)     ... 46 more 

i have 2 parent entities: first theuser subclass userinfo:

public class theuser {       private int userid;          public int getuserid() {             return userid;         }          public void setuserid(int userid) {             this.userid = userid;         }        private string userfirstname;          public string getuserfirstname() {             return userfirstname;         }          public void setuserfirstname(string userfirstname) {             this.userfirstname = userfirstname;         }        private string userlastname;          public string getuserlastname() {             return userlastname;         }          public void setuserlastname(string userlastname) {             this.userlastname = userlastname;         }        private string userpassword;          public string getuserpassword() {             return userpassword;         }          public void setuserpassword(string userpassword) {             this.userpassword = userpassword;         }        private string useremale;          public string getuseremale() {             return useremale;         }          public void setuseremale(string useremale) {             this.useremale = useremale;         }        private string usergender;          public string getusergender() {             return usergender;         }          public void setusergender(string usergender) {             this.usergender = usergender;         }        private list<userevents> usereventslist = new arraylist<userevents>();          public list<userevents> getusereventslist() {             return usereventslist;         }          public void setusereventslist(list<userevents> usereventslist) {             this.usereventslist = usereventslist;         } 

this xml mapping theuser:

<class name="demidov.pkg.domain.theuser" table="user">      <id name="userid" column="id">         <generator class="native"/>     </id>       <discriminator  column="user_info_type" type="string"/>       <property name="userfirstname" column="first_name" type="string"/>     <property name="userlastname" column="last_name" type="string"/>     <property name="userpassword" column="password" type="string"/>     <property name="useremale" column="emale" type="string"/>     <property name="usergender" column="gender" type="string"/>       <list name="usereventslist" inverse="false" lazy="true" fetch="select" cascade="all" >          <key column="user_id" not-null="true" />          <index column="event_index" />                    <one-to-many class="demidov.pkg.domain.userevents"/>              </list>       <subclass name="demidov.pkg.domain.usercontactinfo" discriminator-value="uci" >          <property name="personaddress" column="uci_person_address" />         <property name="personphonenumber" column="uci_person_phone" />      </subclass>   </class> 

and userevents subclasses: computermaintenanceevent , softwaredevelopmentevent entity:

public class userevents {        private int usereventid;          public int getusereventid() {             return usereventid;         }          public void setusereventid(int usereventid) {             this.usereventid = usereventid;         }        private date usereventdate;               public date getusereventdate() {             return usereventdate;         }          public void setusereventdate(date usereventdate) {             this.usereventdate = usereventdate;         }        protected theuser theuser;          public theuser gettheuser() {             return theuser;         }          public void settheuser(theuser theuser) {             this.theuser = theuser;         }  } 

with xml mapping userevent:

<hibernate-mapping>               <class name="demidov.pkg.domain.userevents" table="user_events">                   <id name="usereventid" column="id" type="integer">                     <generator class="native"/>                 </id>                   <property name="usereventdate" column="event_date" type="date"/>                   <many-to-one name="theuser" class="demidov.pkg.domain.theuser" insert="false" update="false" lazy="false" fetch="select" cascade="save-update">                     <column name="user_id" not-null="true" unique="true" />                 </many-to-one>                   <joined-subclass name="demidov.pkg.domain.computermaintenanceevent" table="maintenance_event">                      <key column="maintenance_id" not-null="true"/>                      <property name="descriptionmaintenance" column="description_maintenance" type="string"/>                     <property name="additionalinfomaintenance" column="additional_info_maintenance" type="string"/>                  </joined-subclass>                                 <joined-subclass name="demidov.pkg.domain.softwaredevelopmentevent" table="development_event">                      <key column="development_id" not-null="true"/>                      <property name="descriptiondevelopment" column="description_development" type="string"/>                     <property name="additionalinfodevelopment" column="additional_info_development" type="string"/>                  </joined-subclass>               </class>           </hibernate-mapping> 

in database have data inserted once:

theuser entity + subclass = inheritance table per hierarchy:

+----+----------------------------+------------+-----------+----------+-----------------+--------+--------------------+------------------+ | id | user_info_type             | first_name | last_name | password | emale           | gender | uci_person_address | uci_person_phone | +----+----------------------------+------------+-----------+----------+-----------------+--------+--------------------+------------------+ |  1 | demidov.pkg.domain.theuser | vadim      | demidov   | 123123   | vadim@google.ru | male   | null               | null             | +----+----------------------------+------------+-----------+----------+-----------------+--------+--------------------+------------------+ 

between userevents , subclasses: softwaredevelopmentevent , computermaitnanceevent table per subclass inheritance.

userevents table in db:

+----+------------+---------+-------------+ | id | event_date | user_id | event_index | +----+------------+---------+-------------+ |  1 | 2013-10-07 |       1 |           0 | +----+------------+---------+-------------+ 

subclass of userevents - softwaredevelopmentevent:

+----------------+-------------------------+-----------------------------+ | development_id | description_development | additional_info_development | +----------------+-------------------------+-----------------------------+ |              1 | create website          | create news portal          | +----------------+-------------------------+-----------------------------+ 

i'm trying add existed user new event:

public void saveuser(integer id, list<userevents> listevents) {          session session = sessionfactory.getcurrentsession();          theuser theuser = (theuser)session.get(theuser.class, id);          theuser.setusereventslist(listevents);          session.saveorupdate(theuser);      } 

main method:

list<userevents> eventslist = new arraylist<userevents>();           computermaintenanceevent sde = new computermaintenanceevent();         sde.setdescriptionmaintenance("fix pc");         sde.setadditionalinfomaintenance("fix pc please");         sde.setusereventdate(new date());         eventslist.add(sde);           dao.saveuser(1, eventslist); 

please help. thank best regards.

^solved problem: problem in userevent.hbm.xml had unique="true" set relationships accident.

thank's everybody.

you using native generator class.

<generator class="native"/> 

when use native generator need create sequence in database. create sequence name hibernate_sequence , try , run code.

also because of following code

dao.saveuser(1, eventslist); 

everytime execute program program trying save user id 1, user id 1 exists , constraint fails , exception. need keep id unique if wish handle on own.. else not provide id , let hibernate generate using underlying sequence


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -