Joined: Fri Sep 22, 2006 6:19 am Posts: 1
|
Well, i have hibernate 3.1 mysql 5 and tomcat 5.5
there is a problem in one-to-many relationships between Item and bid
Here is the code
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.3.154:3306/prova</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.jdbc.batch_size">0</property>
<!-- Mapping files -->
<mapping resource="item-hbm.xml"/>
<mapping resource="bid-hbm.xml"/>
</session-factory>
</hibernate-configuration>
Bid
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.Bid" table="BID">
<id name="id" column="BID_ID" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="item_id"/>
<many-to-one name="item" column="ITEM_ID" class="hibernate.Item" not-null="true" />
<property name="amount"/>
</class>
</hibernate-mapping>
Item
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.Item" table="ITEM">
<id name="id" column="ITEM_ID" unsaved-value="0">
<generator class="increment"/>
</id>
<set name="bids" inverse="true" cascade="save-update">
<key column="ITEM_ID"/>
<one-to-many class="hibernate.Bid"/>
</set>
<property name="name"/>
<property name="description"/>
</class>
</hibernate-mapping>
public class Bid {
private int id;
private int item_id;
private int amount;
private Item item;
/** Creates a new instance of Bid */
public Bid() {
}
public void setId(int idset){
this.id=idset;
}
public int getId(){
return id;
}
public void setItem_id(int item_id_set){
this.item_id=item_id_set;
}
public int getItem_id(){
return item_id;
}
public void setAmount(int amountset){
this.amount=amountset;
}
public int getAmount(){
return amount;
}
public void setItem(Item item) {
this.item=item;
}
public Item getItem(){
return item;
}
}
public class Bid {
private int id;
private int item_id;
private int amount;
private Item item;
/** Creates a new instance of Bid */
public Bid() {
}
public void setId(int idset){
this.id=idset;
}
public int getId(){
return id;
}
public void setItem_id(int item_id_set){
this.item_id=item_id_set;
}
public int getItem_id(){
return item_id;
}
public void setAmount(int amountset){
this.amount=amountset;
}
public int getAmount(){
return amount;
}
public void setItem(Item item) {
this.item=item;
}
public Item getItem(){
return item;
}
}
and the test code
Item prodotto = new Item();
Bid offerta = new Bid();
offerta.setAmount(100);
prodotto.setName("prodotto2");
prodotto.setDescription("il prodotto 2");
prodotto.addBid(offerta);
session.save(prodotto);
session.flush();
and the stack error
org.hibernate.exception.SQLGrammarException: could not insert: [hibernate.Bid]
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2078)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)
org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
hibernate.TestServlet.createItem(TestServlet.java:88)
hibernate.TestServlet.doGet(TestServlet.java:34)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
|
|