-->
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: Validation problem with association
PostPosted: Wed Apr 14, 2010 8:21 am 
Newbie

Joined: Wed Apr 14, 2010 5:30 am
Posts: 4
Hello,
i work with these two tables :
model (*modelId, acquisitionCost);
mobile (*mobilId , *modelId);

Here are my mapping files :
Code:
<hibernate-mapping package="market.test.hibernate">
    <class name="Model" table="model" catalog="market">
        <id name="modelId" type="java.lang.Integer">
            <column name="modelId" />
            <generator class="identity" />
        </id>
        <property name="acquisitionCost" type="java.lang.Float">
            <column name="acquisitionCost" precision="12" scale="0" />
        </property>     
        <set name="mobiles" inverse="true" lazy="true" table="mobile" fetch="select">
            <key>
                <column name="modelId" not-null="true" />
            </key>
            <one-to-many class="Mobile" />
        </set>
    </class>
</hibernate-mapping>

and
Code:
<hibernate-mapping package="market.test.hibernate">
    <class name="Mobile" table="mobile" catalog="market">
        <id name="mobileId" type="int">
            <column name="mobileId" />
            <generator class="increment" />
        </id>
        <many-to-one name="model" class="Model" fetch="select">
            <column name="modelId" not-null="false" />
        </many-to-one>
        <property name="configId" type="string">
            <column name="configId" length="45" />
        </property>
        <set name="loadcapacities" inverse="true" lazy="true" table="loadcapacity" fetch="select">
            <key>
                <column name="mobileId" not-null="true" />
            </key>
            <one-to-many class="Loadcapacity" />
        </set>
    </class>
</hibernate-mapping>

I have an entry in my table model(modelId=1, acquisitionCost=3000),
and two in the table mobile (mobilId=1,modelId=null et mobilId=2, modelId=null).
I want to update mobile table by setting the field modelId of the two entries with the value 1.

This is my code :
Code:
private void addMobileToModel(int mobileId, int modelId) {
           Session session = HibernateUtil.getSessionFactory().getCurrentSession();
           session.beginTransaction();
           Mobile mobile = (Mobile) session.load(Mobile.class, mobileId);
           Model model = (Model) session.load(Model.class, modelId);            
           model.getMobiles().add(mobile);
           session.getTransaction().commit();
}

When I call this code, there is no error but my Data base is not updated.
Does anyone has an idea ?

Thanks.


Top
 Profile  
 
 Post subject: Re: Validation problem with association
PostPosted: Wed Apr 14, 2010 2:34 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
because the way you have set the association by referring to inverse="true" in the class Model. The inverse end that is, the Mobile entity is responsible for what models can be associated to them.

Code:
<set name="mobiles" inverse="true" lazy="true" table="mobile" fetch="select">
            <key>
                <column name="modelId" not-null="true" />
            </key>
            <one-to-many class="Mobile" />
        </set>


So this wont work.
model.getMobiles().add(mobile);
As the Model object is not going to update it.


Instead its the mobile.set(model)
Hope this helps!


Top
 Profile  
 
 Post subject: Re: Validation problem with association
PostPosted: Thu Apr 15, 2010 3:23 am 
Newbie

Joined: Wed Apr 14, 2010 5:30 am
Posts: 4
OK thanks, I will try to change the inverse tag.
But, I tried to change my code with :
Code:
mobile.setModel(model);


And I get this error

Code:
Cannot add or update a child row: a foreign key constraint fails (`market`.`mobile`, CONSTRAINT `fk_mobile_model` FOREIGN KEY (`modelId`) REFERENCES `model` (`modelId`) ON DELETE NO ACTION ON UPDATE NO ACTION)
- Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
   at market.test.Test.addMobileToModel(Test.java:85)
   at market.test.Test.main(Test.java:16)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`market`.`mobile`, CONSTRAINT `fk_mobile_model` FOREIGN KEY (`modelId`) REFERENCES `model` (`modelId`) ON DELETE NO ACTION ON UPDATE NO ACTION)
   at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2018)
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1454)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   ... 9 more
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
   at market.test.Test.addMobileToModel(Test.java:85)
   at market.test.Test.main(Test.java:16)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`market`.`mobile`, CONSTRAINT `fk_mobile_model` FOREIGN KEY (`modelId`) REFERENCES `model` (`modelId`) ON DELETE NO ACTION ON UPDATE NO ACTION)
   at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2018)
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1454)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   ... 9 more


Do you know why?

Thanks a lot.


Top
 Profile  
 
 Post subject: Re: Validation problem with association
PostPosted: Thu Apr 15, 2010 4:01 am 
Newbie

Joined: Wed Apr 14, 2010 5:30 am
Posts: 4
OK, this error was due because I had a table in InnoDB and the other one in MyIsam. If I put my twos tables in InnoDB, your solution is working.

Thanks.


Top
 Profile  
 
 Post subject: Re: Validation problem with association
PostPosted: Thu Apr 15, 2010 3:11 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
I'm glad the solution worked for you. But I have a question on your domain Object. I might be wrong.. but Is'nt it that the Mobile should have Set of Models and the Model corresponds to a given Mobile? I see it other way around in your mappings. I'm curious to know why its other way around.


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.