-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: many-to-many with atributte: does not save on join table.
PostPosted: Tue Aug 03, 2004 4:30 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
Hi, I have a many-to-many relationship between Purchase and Goods and the join table has a atributte named by quantity. When I save a Purchase object, the data on join table is not saved. My codes are:
[code]
public class RelationshipPurchaseGoods implements Serializable {

/**
*
*/
private Goods goods;

/**
*
*/
private int quantity;

/**
* Returns the value of {@link #goods goods} to this object.
*
* @hibernate.many-to-one cascade="all"
* class="br.ufpe.scacin.model.Goods"
* column = "FK_GOODS_ID"
* not-null="true"
*
* @return Returns the goods.
*/
public Goods getGoods() {
return this.goods;
}

/**
* Sets the value of {@link #goods goods} to this object.
*
* @param goods The goods to set.
*/
public void setGoods(Goods goods) {
this.goods = goods;
}

/**
* Returns the value of {@link #quantity quantity} to this object.
*
* @hibernate.property column="JOIN_PURCHASE_GOODS_QUANTITY"
* not-null="true"
* type="int"
* update="true"
*
* @return Returns the quantity.
*/
public int getQuantity() {
return this.quantity;
}

/**
* Sets the value of {@link #quantity quantity} to this object.
*
* @param quantity The quantity to set.
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}

/**
* Testa se ambos os objetos s

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 12:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Try changing the inverse attribute to false on the <bag/> element.

I'm not sure what the inverse would really be in a component collection.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 6:49 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
Hi, thanks for your response, steve. Now, Hibernate try save data on the join table. But, the following error occur:

Code:
Hibernate: insert into JOIN_PURCHASE_GOODS (FK_PURCHASE_ID, FK_GOODS_ID, JOIN_PURCHASE_GOODS_QUANTITY) values (?, ?, ?)
04/08/2004 19:35:42 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1048, SQLState: S1000
04/08/2004 19:35:42 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: General error,  message from server: "Column 'FK_GOODS_ID' cannot be null"
04/08/2004 19:35:42 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1048, SQLState: S1000
04/08/2004 19:35:42 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: General error,  message from server: "Column 'FK_GOODS_ID' cannot be null"
04/08/2004 19:35:42 net.sf.hibernate.JDBCException <init>
SEVERE: Could not execute JDBC batch update
java.sql.BatchUpdateException: General error,  message from server: "Column 'FK_GOODS_ID' cannot be null"
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1367)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.executeBatch(DelegatingPreparedStatement.java:231)
   at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
   at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2417)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:386)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:316)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:211)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148)
   at $Proxy2.insert(Unknown Source)
   at br.ufpe.scacin.manager.PurchaseManager.insert(PurchaseManager.java:95)
   at br.ufpe.scacin.SCACInFacade.insert(SCACInFacade.java:314)

   at br.ufpe.scacin.SCACInFacade.main(SCACInFacade.java:506)
04/08/2004 19:35:42 net.sf.hibernate.impl.SessionImpl execute
SEVERE: Could not synchronize database state with session
net.sf.hibernate.JDBCException: Could not execute JDBC batch update
   at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2417)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:386)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:316)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:211)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148)
   at $Proxy2.insert(Unknown Source)
   at br.ufpe.scacin.manager.PurchaseManager.insert(PurchaseManager.java:95)
   at br.ufpe.scacin.SCACInFacade.insert(SCACInFacade.java:314)
   at br.ufpe.scacin.SCACInFacade.main(SCACInFacade.java:506)
Caused by: java.sql.BatchUpdateException: General error,  message from server: "Column 'FK_GOODS_ID' cannot be null"
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1367)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.executeBatch(DelegatingPreparedStatement.java:231)
   at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
   at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
   ... 13 more
04/08/2004 19:35:42 org.springframework.jdbc.support.SQLStateSQLExceptionTranslator translate
INFO: Translating SQLException with SQLState 'S1000' and errorCode '1048' and message [General error,  message from server: "Column 'FK_GOODS_ID' cannot be null"]; SQL was [null] for task [HibernateTemplate]
org.springframework.jdbc.UncategorizedSQLException: (HibernateTemplate): encountered SQLException [General error,  message from server: "Column 'FK_GOODS_ID' cannot be null"]; nested exception is java.sql.BatchUpdateException: General error,  message from server: "Column 'FK_GOODS_ID' cannot be null"
java.sql.BatchUpdateException: General error,  message from server: "Column 'FK_GOODS_ID' cannot be null"
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1367)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.executeBatch(DelegatingPreparedStatement.java:231)
   at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
   at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2417)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
   at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:386)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:316)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:211)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148)
   at $Proxy2.insert(Unknown Source)
   at br.ufpe.scacin.manager.PurchaseManager.insert(PurchaseManager.java:95)
   at br.ufpe.scacin.SCACInFacade.insert(SCACInFacade.java:314)
   at br.ufpe.scacin.SCACInFacade.main(SCACInFacade.java:506)

So, a try to set the cascade property to save-update on the relationship object like this:
Code:
    /**
     * Returns the value of {@link #goods} to this object.
     *
     * @hibernate.many-to-one cascade="save-update"
     *                        class="br.ufpe.scacin.model.Goods"
     *                        column = "FK_GOODS_ID"
     *                        not-null="true"
     *
     * @return Returns the goods.
     */
    public Goods getGoods() {
        return this.goods;
    }

I read the faq but a can not see how to solve it.

valeuz...

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 6:50 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
Erhm, my Purchase.hbm.xml file is (where is the edit button?):
Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="br.ufpe.scacin.model.Purchase"
        table="SCACIN_PURCHASE"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="PURCHASE_ID"
            type="long"
            unsaved-value="-1"
        >
            <generator class="native">
            </generator>
        </id>

        <many-to-one
            name="costumer"
            class="br.ufpe.scacin.model.User"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="USER_ID"
            not-null="true"
        />

        <property
            name="date"
            type="calendar"
            update="true"
            insert="true"
            access="property"
            column="PURCHASE_DATE"
            not-null="true"
        />

        <property
            name="origin"
            type="string"
            update="true"
            insert="true"
            access="property"
            column="PURCHASE_ORIGIN"
            not-null="true"
        />

        <bag
            name="relationships"
            table="JOIN_PURCHASE_GOODS"
            lazy="true"
            inverse="false"
            cascade="all"
        >

              <key
                  column="FK_PURCHASE_ID"
              >
              </key>

              <composite-element
                  class="br.ufpe.scacin.model.RelationshipPurchaseGoods"
              >

        <many-to-one
            name="goods"
            class="br.ufpe.scacin.model.Goods"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="FK_GOODS_ID"
            not-null="true"
        />

        <property
            name="quantity"
            type="int"
            update="true"
            insert="true"
            access="property"
            column="JOIN_PURCHASE_GOODS_QUANTITY"
            not-null="true"
        />

              </composite-element>

        </bag>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Purchase.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

        <query name="PurchaseFindByOrigin"><![CDATA[
            from Purchase p where p.origin = ?
        ]]></query>
        <query name="PurchaseFindByDate"><![CDATA[
            from Purchase p where p.date between ? and ?
        ]]></query>
        <query name="PurchaseFindByCostumer"><![CDATA[
            from Purchase p inner join p.costumer c where c.id = ?
        ]]></query>
        <query name="PurchaseExist"><![CDATA[
            from Purchase p inner join p.costumer c where c.id = ? and p.origin = ?
        ]]></query>
        <query name="PurchaseCount"><![CDATA[
            select count(p.id) from Purchase p
        ]]></query>
        <query name="PurchaseMax"><![CDATA[
            select max(p.id) from Purchase p
        ]]></query>

</hibernate-mapping>

valeuz...

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2004 9:21 am 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
Hi, sorry, I know thas is not so much graceful to up a question, but I could not solve this question alone. So, I hope that anyone help me with it.

valeuz...

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.