Je suis nouveau à hiberner et j'essaie un exemple de mappage dans xml. Je suis au-dessus d'erreur pendant l'exécution de l'application s'il vous plaît aider. voici le code que je l'ai écritcom.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Impossible d'ajouter ou de mettre à jour une ligne enfant
Voici les cours de cartographie
de 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>
Voici les classes POJO i ont utilisé pour le mappage
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);
}
merci d'avance!
ici est un message d'erreur
Impossible d'ajouter ou mettre à jour une ligne enfant: une contrainte de clé étrangère échoue (.
hibernate_assign
quotation
, CONTRAINTEFKA771958CD5030893
FOREIGN KEY (USER_ID
) RÉFÉRENCESuser
(USER_ID
)) Exception dans thread "principal" org.hibernate.exception.ConstraintViolationException: n'a pas pu insérer: [main.com.paramatrix.models.Quotation] à 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) à org.hibernate.engine.ActionQueue.execute (ActionQueue.java:273) à 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) au org.hibernate.impl.SessionImpl.save (SessionImpl.java: 701) à org.hibernate.impl.SessionImpl.save (SessionImpl.java:697) à main.com.paramatrix.util.DataProvider.addData (DataProvider.java:10) à main.com.paramatrix. util.App.main (App.java:49) causée par: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Impossible d'ajouter ou mettre à jour une ligne enfant: une contrainte de clé étrangère échoue (hibernate_assign
quotation
, cONTRAINTEFKA771958CD5030893
. FOREIGN KEY (USER_ID
) RÉFÉRENCESuser
(USER_ID
)) à sun.reflect.NativeConstructorAccessorImpl.newInstance0 (native Method) à sun.reflect.NativeConstructorAccessorImpl.newInstance (Information inconnue Source) à sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Information inconnue Source) à java.lang.reflect.Constructor.newInstance (Source inconnue) à com.mysql.jdbc.Util.handleNewInstance (Util.java:425) à com.mysql.jdbc.Util.getInstance (Util.java:408) à com.mysql.jdbc.SQLError.createSQLException (SQLError.java:935) à com.mysql.jdbc.MysqlIO.checkErrorPacket (MysqlIO.java : 3973) à com.mysql.jdbc.MysqlIO.checkErrorPacket (MysqlIO.java:3909) à com.mysql.jdbc.MysqlIO.sendCommand (MysqlIO.java:2527) à com.mysql.jdbc.MysqlIO.sqlQueryDirect (MysqlIO.java:2680) à com.mysql.jdbc.ConnectionImpl.execSQL (ConnectionImpl.java:2501) à 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)
pouvez-vous ajouter la trace de la pile? –
vient d'éditer la question –
L'erreur signifie que vous ne respectez pas une contrainte de clé étrangère, donc vous essayez probablement d'ajouter ou de mettre à jour l'utilisateur avant d'ajouter ou de mettre à jour un devis! où est le code qui génère cette erreur? –