-->
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.  [ 10 posts ] 
Author Message
 Post subject: Problems with cascaded storeage
PostPosted: Wed Aug 18, 2004 7:20 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
Hibernate version:2.1

Mapping documents:
City.hbm.xml
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.test.yellowPages.model.impl.CityImpl" table="CITY">
<id name="cityId">
<column name="ID_CITY" />
<generator class="increment" />
</id>

<property name="cityName">
<column name="NAME" />
</property>

<many-to-one name="country"
column="ID_COUNTRY"
class="com.test.yellowPages.model.impl.CountryImpl"
not-null="true" />

</class>
</hibernate-mapping>

Country.hbm.xml
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.test.yellowPages.model.impl.CountryImpl" table="COUNTRY">
<id name="countryId">
<column name="ID_COUNTRY" />
<generator class="increment" />
</id>

<property name="countryName">
<column name="NAME" />
</property>

<set name="cities"
inverse="true"
cascade="save-update" >

<key column="ID_COUNTRY" />
<one-to-many class="com.test.yellowPages.model.impl.CityImpl" />

</set>

</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Session session = mSessionFactory.openSession();
Transaction tx = session.beginTransaction();
CountryImpl ci = new CountryImpl();
ci.setCountryName("China");
City ci1 = new CityImpl();
ci1.setCityName("Beijing");
ci.setCity(ci1);
City ci2 = new CityImpl();
ci2.setCityName("Shanghai");
ci.setCity(ci2);
session.save(ci);
tx.commit();
session.close();


Name and version of the database you are using:Oracle 9



I have the following problem. I would like to generate a Country and add some cities to the country. But i can't store it cascaded. Can anybodz tell my why ?? Is it possible that i have some problems with the keys ??

The code of my CityImpl class looks that:
Code:
public class CityImpl
    implements City
{
    private long mCityId;
    private String mCityName;
    private Country mCountry;

    //Default Constructor
    public CityImpl()
    {
    }

    public CityImpl(String aCityName,
                    Country aCountry)
    {
        mCityName = aCityName;
        mCountry = aCountry;
    }

    public CityImpl(long aCityId,
                    String aCityName,
                    Country aCountry)
    {
        this(aCityName, aCountry);
        mCityId = aCityId;
    }

    public void setCityId(long aCityId)
    {
        mCityId = aCityId;
    }

    public long getCityId()
    {
        return mCityId;
    }

    public void setCityName(String aCityName)
    {
        mCityName = aCityName;
    }

    public String getCityName()
    {
        return mCityName;
    }

    public void setCountry(Country aCountry)
    {
        mCountry = aCountry;
    }

    public Country getCountry()
    {
        return mCountry;
    }
}


The code of my CountryImpl Class looks that:
Code:
public class CountryImpl
    implements Country
{
    private long mCountryId;
    private String mCountryName;
    private Set mCities = new HashSet();
    private static Logger mLogger =
        Logger.getLogger("com.test.yellowPages.model.impl.CountryImpl");

    public CountryImpl()
    {   
    }

    public CountryImpl(long aCountryId,
                       String aCountryName)
    {
        mCountryId = aCountryId;
        mCountryName = aCountryName;
    }


    public void setCountryId(long aCountryId)
    {
        mCountryId = aCountryId;
    }

    public long getCountryId()
    {
        return mCountryId;
    }
 
    public void setCountryName(String aCountryName)
    {
        mCountryName = aCountryName;
    }

    public String getCountryName()
    {
        return mCountryName;
    }

    public void setCities(Set aCities)
    {
        mCities = aCities;
    }

    public Set getCities()
    {
        return mCities;
    }

    public void setCity(City aCity)
    {
        mLogger.debug("City added -> name = "+aCity.getCityName());
        aCity.setCountry(this);
        mCities.add(aCity);
    }

    public int getNumberOfCities()
    {
        return mCities.size();
    }

}


Thanks for your help !!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 7:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
can you give stacktrace, especially sql

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:01 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
hi !

thats my stack trace:
Code:
a different object with the same identifier value was already associated with the session: 0, of class: com.test.yellowPages.model.impl.CityImpl
   at com.test.yellowPages.model.impl.DatabaseManagerImpl.addCountry(DatabaseManagerImpl.java:132)
   at com.test.yellowPages.TestHibernateAction.execute(TestHibernateAction.java:55)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
   at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
   at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
   at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
   at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
   at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
   at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
   at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
   at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
   at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
   at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
   at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
   at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
   at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
   at java.lang.Thread.run(Thread.java:534)



Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:10 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
can you use sequence instead of increment?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:18 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
i tried to use a sequence - but it seems that i am to stupid for it
can you tell me how to do it ??

my database instance is:
km

and my sequence is named:
city_sequence

and
country_sequence

how does the Country.hbm.xml has to look like ??

another reason why i wanted to use an increment instead of a sequence is because then i can easily switch to mysql without changing anything in the config file.

regards,
martin

PS: Hibernate is a very useful tool. Thanks a lot to all your developers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
1- if you want to use seuance, then go to reference guide, it is well documented. The only thing you have to know is taht the nextVal will be executed at flushtime (most of the time = at commit) for a call to session.save(objUsingSequenceGeneratorAsId)
2- about increment, i've never used it, i don't know why this generator doesn't fill the id well
3- about switching from oracle to mysql, use generator = native, it will use sequence under oracle and identity under mysql

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:39 am 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
hi !

i have used a sequence now and i still get the same failure message - and i have absolutely no idea why.

i have also tried to completely empty the database tables.


regards,
martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:48 am 
Newbie

Joined: Wed Aug 18, 2004 8:44 am
Posts: 12
Hi kook!

Try using your ID as type Long instead of long..
Dont use primitive types because it doesnt accept NULL values. I think thats causing the error

Hope it helped =)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
of course Macio is right

you can keep long but define unsaved value = 0

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 1:48 pm 
Newbie

Joined: Wed Aug 18, 2004 6:30 am
Posts: 14
Hi !

Thanks a lot ! That was my problem!
It works fine now.

regards,
martin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.