2017-10-18 89 views
0

我是新来的休眠,我想在xml映射的示例。我在运行应用程序时遇到以上错误,请帮助。这里是我写com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行

这里是映射类

quotation.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="main.com.paramatrix.models.Quotation" table="quotation"> 
     <id name="quotationId" type="long"> 
      <column name="QUOTATION_ID" /> 
      <generator class="identity" /> 
     </id> 
     <property column="NO_OF_ITEMS" name="noOfItems" type="long" /> 
     <property column="QUOTATION_GEN_DATE" name="quotationGenDate" 
      type="string" /> 
     <one-to-one name="invoice" class="main.com.paramatrix.models.Invoice" 
      cascade="save-update"></one-to-one> 

     <set name="itemList" table="item" inverse="true" lazy="true" 
      fetch="select"> 
      <key> 
       <column name="QUOTATION_ID" not-null="true" /> 
      </key> 
      <one-to-many class="main.com.paramatrix.models.Item" /> 
     </set> 


     <many-to-one name="user" class="main.com.paramatrix.models.User" 
      fetch="select"> 
      <column name="USER_ID" not-null="true" /> 
     </many-to-one> 

    </class> 
</hibernate-mapping> 

user.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="main.com.paramatrix.models.User" table="user"> 
     <id name="userId" type="long"> 
      <column name="USER_ID" /> 
      <generator class="identity" /> 
     </id> 
     <property column="USER_NAME" name="userName" type="string" /> 

     <set name="quotationList" table="quotation" inverse="true" lazy="true" 
      fetch="select"> 
      <key> 
       <column name="USER_ID" not-null="true" /> 
      </key> 
      <one-to-many class="main.com.paramatrix.models.Quotation" /> 
     </set> 
    </class> 
</hibernate-mapping> 

这里是POJO类我的代码已用于制图

User.java

public class User implements Serializable{ 

    private long userId ; 
    private String userName; 
    private Set<Quotation> quotationList = new HashSet<Quotation>(); 
    public long getUserId() { 
     return userId; 
    } 
    public void setUserId(long userId) { 
     this.userId = userId; 
    } 
    public String getUserName() { 
     return userName; 
    } 
    public void setUserName(String userName) { 
     this.userName = userName; 
    } 
    public Set<Quotation> getQuotationList() { 
     return quotationList; 
    } 
    public void setQuotationList(Set<Quotation> quotationList) { 
     this.quotationList = quotationList; 
    } 

} 


Quotation.java 

    public class Quotation implements Serializable{ 

     private long quotationId; 
     private long noOfItems; 
     private String quotationGenDate; 
     private Set<Item> itemList = new HashSet<Item>(); 
     private Invoice invoice; 
     private User user; 
     public long getQuotationId() { 
      return quotationId; 
     } 
     public void setQuotationId(long quotationId) { 
      this.quotationId = quotationId; 
     } 
     public long getNoOfItems() { 
      return noOfItems; 
     } 
     public void setNoOfItems(long noOfItems) { 
      this.noOfItems = noOfItems; 
     } 
     public String getQuotationGenDate() { 
      return quotationGenDate; 
     } 
     public void setQuotationGenDate(String quotationGenDate) { 
      this.quotationGenDate = quotationGenDate; 
     } 
     public Set<Item> getItemList() { 
      return itemList; 
     } 
     public void setItemList(Set<Item> itemList) { 
      this.itemList = itemList; 
     } 
     public Invoice getInvoice() { 
      return invoice; 
     } 
     public void setInvoice(Invoice invoice) { 
      this.invoice = invoice; 
     } 
     public User getUser() { 


      return user; 
      } 
      public void setUser(User user) { 
       this.user = user; 
      } 


     } 
App.js 
public class App { 

    public static void main(String args[]) { 
     User user = new User(); 
     user.setUserName("Pradip"); 

     Quotation quotation = new Quotation(); 
     Item item1 = new Item(); 
     item1.setItemName("laptop"); 
     Item item2 = new Item(); 
     item2.setItemName("mobile"); 
     // Set<Item> itemList = new HashSet<>(); 

     // itemList.add(item1); 
     // itemList.add(item2); 

     // quotation.setNoOfItems(itemList.size()); 
     quotation.setUser(user); 
     quotation.getItemList().add(item1); 
     quotation.getItemList().add(item2); 
     quotation.setNoOfItems(quotation.getItemList().size()); 

     Invoice invoice = new Invoice(); 
     invoice.setInvoiceGenDate("13-10-2017"); 
     invoice.setTotalBill(50000); 
     quotation.setInvoice(invoice); 
     invoice.setQuotation(quotation); 
     // Set<Quotation> quotationos = new HashSet<Quotation>(); 
     // quotationos.add(quotation); 
     user.getQuotationList().add(quotation); 

     DataProvider.addData(main.com.paramatrix.models.User.class, user); 

     DataProvider.addData(main.com.paramatrix.models.Quotation.class, quotation); 

     DataProvider.addData(main.com.paramatrix.models.Invoice.class, invoice); 

    } 

在此先感谢!

这里是错误消息

不能添加或更新子行,外键约束失败 (hibernate_assignquotation,约束FKA771958CD5030893 外键(USER_ID)参考文献userUSER_ID))在 异常线程“main”org.hibernate.exception.ConstraintViolationException: 无法插入:[main.com.paramatrix.models.Quotation] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:63) 在 org.hibernate.persister.entity .AbstractEntityPersister.insert(AbstractEntityPersister.java:2346) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2853) 在 org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71 ) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(Abs tractSaveEventListener.java:320) 在 org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203) 在 org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) 在 org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) 在 org.hibernate.event.def .DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) 在org.hibernate.impl.SessionImpl。 fireSave(SessionImpl.java:713) org.hibernate.impl.SessionImpl.save(SessionImpl。java:701)at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)at main.com.paramatrix.util.DataProvider.addData(DataProvider.java:10) at main.com.paramatrix。 util.App.main(App.java:49)引起的: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 不能添加或更新子行,外键约束失败 (hibernate_assignquotation,约束FKA771958CD5030893。 外键(USER_ID)参考文献userUSER_ID))在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)at com.mysql.jdbc.Util.getInstance(Util.java:408)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java :3973)处 com.mysql.jdbc.MysqlIO.sqlQueryDirect在 com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)) (MysqlIO.java:2680)at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2501)at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) 在 com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079) 在 com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement的的.java:2013) 在 com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104) 在 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998) 在 org.hibernate作为。 id.IdentityGenerator $ GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:93) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractRetu rningDelegate.java:56)

+0

你可以添加堆栈跟踪吗? –

+0

刚刚编辑问题 –

+0

错误表示您不尊重外键约束,因此您可能在添加或更新引用之前尝试添加或更新用户!产生这个错误的代码在哪里? –

回答

0

这里是我的方法:

首先,你需要更新你的映射文件报价:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="main.com.paramatrix.models.Quotation" table="quotation"> 
     <id name="quotationId" type="long"> 
      <column name="QUOTATION_ID" /> 
      <generator class="identity" /> 
     </id> 
     <property column="NO_OF_ITEMS" name="noOfItems" type="long" /> 
     <property column="QUOTATION_GEN_DATE" name="quotationGenDate" 
      type="string" /> 
     <one-to-one name="invoice" class="main.com.paramatrix.models.Invoice" 
      cascade="save-update"></one-to-one> 

     <set name="itemList" table="item" inverse="true" lazy="true" 
      fetch="select"> 
      <key> 
       <column name="QUOTATION_ID" not-null="true" /> 
      </key> 
      <one-to-many class="main.com.paramatrix.models.Item" /> 
     </set> 


     <many-to-one name="user" class="main.com.paramatrix.models.User" 
      fetch="select"> 
      <column name="USER_ID" not-null="false" /> 
     </many-to-one> 

    </class> 
</hibernate-mapping> 

,然后更新您的主:

public static void main(String args[]) { 
    User user = new User(); 
    user.setUserName("Pradip"); 

    Quotation quotation = new Quotation(); 
    Item item1 = new Item(); 
    item1.setItemName("laptop"); 
    Item item2 = new Item(); 
    item2.setItemName("mobile"); 
    // Set<Item> itemList = new HashSet<>(); 

    // itemList.add(item1); 
    // itemList.add(item2); 

    // quotation.setNoOfItems(itemList.size()); 
    quotation.setUser(user); 
    quotation.getItemList().add(item1); 
    quotation.getItemList().add(item2); 
    quotation.setNoOfItems(quotation.getItemList().size()); 

    Invoice invoice = new Invoice(); 
    invoice.setInvoiceGenDate("13-10-2017"); 
    invoice.setTotalBill(50000); 
    quotation.setInvoice(invoice); 
    invoice.setQuotation(quotation); 
    DataProvider.addData(main.com.paramatrix.models.Quotation.class, quotation); 
user.getQuotationList().add(quotation); 
    DataProvider.addData(main.com.paramatrix.models.User.class, user); 



    DataProvider.addData(main.com.paramatrix.models.Invoice.class, invoice); 

} 

编辑:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="main.com.paramatrix.models.User" table="user"> 
     <id name="userId" type="long"> 
      <column name="USER_ID" /> 
      <generator class="identity" /> 
     </id> 
     <property column="USER_NAME" name="userName" type="string" /> 

     <set name="quotationList" table="quotation" inverse="true" lazy="true" cascade="all" fetch="select"> 
      <key> 
       <column name="USER_ID" not-null="true" /> 
      </key> 
      <one-to-many class="main.com.paramatrix.models.Quotation" /> 
     </set> 
    </class> 
</hibernate-mapping> 
+0

线程“main”org.hibernate中的异常。TransientObjectException:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:main.com.paramatrix.models.User –

+0

我编辑了我的答案! –

+0

同样的错误,我越来越 –

相关问题