-->
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.  [ 35 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Many-to-one relationship problem
PostPosted: Mon Nov 10, 2003 7:36 pm 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
I am evaluating Hibernate for a critical OLTP application. I am developing a Proof-Of-Concept. I am making some silly mistakes being a newbie to Hobernate.

My error is similar to the one in this topic.
http://forum.hibernate.org/viewtopic.ph ... ttype+wrap

But I could not find any clue. Pl help me.



My Parent Class is

Code:
package com.media.myEVNTS;

import java.util.List;
import java.util.ArrayList;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class DistributorProgram {
  String programID;
  String programName;
  int duration;
  List episodes;

  public DistributorProgram() {
    episodes = new ArrayList();
  }

  public DistributorProgram(String programID, String programName, int duration) {
    this();
    this.programID = programID;
    this.programName = programName;
    this.duration = duration;
  }

  public DistributorProgram(String programName, int duration) {
    this();
    this.programName = programName;
    this.duration = duration;
  }

  public void addEpisode(Episode episode) {
    if (episodes == null) {
      episodes = new ArrayList();
    }
    episode.setDistributorProgram(this);
    episodes.add(episodes);
  }

  public void setEpisodes(List episodes){
    this.episodes=episodes;
  }

  public List getEpisodes(){
    return this.episodes;
  }

  public void removeEpisode(Episode episode) {
    //to be implemented

  }

  public int getEpisodeCount() {
    if (episodes != null) {
      return episodes.size();
    }
    return 0;
  }



  public void setProgramName(String programName) {
    this.programName = programName;
  }

  public String getProgramName() {
    return this.programName;
  }

  public void setProgramID(String programID) {
    this.programID = programID;
  }

  public String getProgramID() {
    return this.programID;
  }

  public void setDuration(int programID) {
    this.duration = duration;
  }

  public int getDuration() {
    return this.duration;
  }

}



My Child class is

Code:
package com.media.myEVNTS;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class Episode {
  String episodeID;
  String episodeName;
  int episodeNo;
  DistributorProgram distributorProgram;

  public Episode() {
  }

  public Episode(String episodeID, String episodeName, int episodeNo) {
    this.episodeID = episodeID;
    this.episodeName = episodeName;
    this.episodeNo = episodeNo;
  }

  public Episode(String episodeName, int episodeNo) {
    this.episodeName = episodeName;
    this.episodeNo = episodeNo;
  }

  public void setEpisodeID(String episodeNo) {
    this.episodeID = episodeID;
  }

  public String getEpisodeID() {
    return this.episodeID;
  }

  public void setEpisodeName(String episodeName) {
    this.episodeName = episodeName;
  }

  public String getEpisodeName() {
    return this.episodeName;
  }

  public void setEpisodeNo(int episodeNo) {
    this.episodeNo = episodeNo;
  }

  public int getEpisodeNo() {
    return this.episodeNo;
  }

  public void setDistributorProgram(DistributorProgram program) {
    this.distributorProgram = program;
  }

  public DistributorProgram getDistributorProgram() {
    return this.distributorProgram;
  }

}



My Mapping file is

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="com.media.myEVNTS.DistributorProgram" table="dist_program">

        <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->
   <id name="programID" type="string" unsaved-value="null" >
            <column name="PROGRAM_ID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>
      <!-- A program has to have a name, but it shouldn' be too long. -->
      <property name="programName">
           <column name="PROGRAM_NAME" sql-type="varchar(16)" not-null="true"/>
      </property>

      <!-- A program has to have a name, but it shouldn' be too long. -->
      <property name="duration">
           <column name="PROGRAM_DURATION" sql-type="int" not-null="true"/>
      </property>
   <set name = "episodes" inverse="true" lazy="true" cascade="all">
      <key column="PROGRAM_ID"/>    
      <one-to-many class="com.media.myEVNTS.Episode"/>
   </set>
    </class>

   <class name="com.media.myEVNTS.Episode" table="episode" dynamic-update="true">      
      <id name="episodeID" column="EPISODE_ID" type="string" unsaved-value="null">
         <column name="EPISODE_ID" sql-type="char(32)" not-null="true"/>
         <generator class="uuid.hex"/>
      </id>      
      <property name="episodeName">
              <column name="EPISODE_NAME" sql-type="varchar(16)"/>
         </property>

      <property name="episodeNo">
           <column name="EPISODE_NO" sql-type="int"/>
         </property>
      <many-to-one name="distributorProgram" column="PROGRAM_ID" not-null="true"/>

    </class>


</hibernate-mapping>



The error I am getting is as follows:


Quote:
8:21:24,882 INFO [STDOUT] Running Test 1
8:21:25,101 ERROR [STDERR] java.lang.ClassCastException
8:21:25,101 ERROR [STDERR] at net.sf.hibernate.type.SetType.wrap(SetType.ja
a:27)
8:21:25,101 ERROR [STDERR] at net.sf.hibernate.impl.SessionImpl.wrap(Sessio
Impl.java:2519)
8:21:25,288 ERROR [STDERR] at net.sf.hibernate.impl.SessionImpl.wrap(Sessio
Impl.java:2459)
8:21:25,288 ERROR [STDERR] at net.sf.hibernate.impl.SessionImpl.doSave(Sess
onImpl.java:708)
8:21:25,288 ERROR [STDERR] at net.sf.hibernate.impl.SessionImpl.save(Sessio
Impl.java:605)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 7:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You have mapped a property of type List as a <set>


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Mon Nov 10, 2003 8:52 pm 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
Thank you Gavin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 9:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No problem. Good Luck!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 11:32 am 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
Now I am getting this error:

Code:
10:20:48,602 INFO  [STDOUT] Running Test 1
10:20:48,743 ERROR [STDERR] net.sf.hibernate.MappingException: No persister for:
java.util.ArrayList
        at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryI
mpl.java:420)
        at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2302)

        at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2309)

        at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1195)

        at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:88)
        at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:258)
        at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:298)
        at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:341)
        at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:739)
        at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)



I changed the mapping of Set to List and this is my schema:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="com.media.myEVNTS.DistributorProgram" table="dist_program">

        <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->
   <id name="programID" type="string" unsaved-value="null" >
            <column name="PROGRAM_ID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>
      <!-- A program has to have a name, but it shouldn' be too long. -->
      <property name="programName">
           <column name="PROGRAM_NAME" sql-type="varchar(16)" not-null="true"/>
      </property>

      <!-- A program has to have a name, but it shouldn' be too long. -->
      <property name="duration">
           <column name="PROGRAM_DURATION" sql-type="int" not-null="true"/>
      </property>
   <list name = "episodes" inverse="true" lazy="true" cascade="all">
      <key column="PROGRAM_ID"/>
      <index column="EPISODE_NO"/>   
      <one-to-many class="com.media.myEVNTS.Episode"/>
   </list>
    </class>

   <class name="com.media.myEVNTS.Episode" table="episode" dynamic-update="true">      
      <id name="episodeID" column="EPISODE_ID" type="string" unsaved-value="null">
         <column name="EPISODE_ID" sql-type="char(32)" not-null="true"/>
         <generator class="uuid.hex"/>
      </id>      
      <property name="episodeName">
              <column name="EPISODE_NAME" sql-type="varchar(16)"/>
         </property>

      <property name="episodeNo">
           <column name="EPISODE_NO" sql-type="int"/>
         </property>
      <many-to-one name="distributorProgram" column="PROGRAM_ID" not-null="true"/>

    </class>


</hibernate-mapping>



Please help me out. Is there any source where I can get more examples of Hibernate relationships than those in Chapter 10 of the documentation. I feel, if we have more examples, we(newbies) can fight more before posting to the forum.

Thank you.


Top
 Profile  
 
 Post subject: Addendum to the above post
PostPosted: Tue Nov 11, 2003 11:35 am 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
My database schema is

CREATE TABLE dbo.dist_program
(
PROGRAM_ID char(32) NOT NULL,
PROGRAM_NAME varchar(16) NULL,
PROGRAM_DURATION int NULL,
CONSTRAINT dist_progr_PROGRA_8299620331
PRIMARY KEY CLUSTERED (PROGRAM_ID)
)


CREATE TABLE dbo.episode
(
EPISODE_ID char(32) NOT NULL,
PROGRAM_ID_FK char(32) NOT NULL,
EPISODE_NAME varchar(16) NULL,
EPISODE_NO int NULL,
CONSTRAINT episode_8619621471
PRIMARY KEY CLUSTERED (EPISODE_ID)
)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 12:46 pm 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
The following lines I got from FAQs.
Quote:
What column should I map the <index> tag of an array or List to?You need a seperate table column holding the array or List index (the i in foo[i])! If your relational model doesn't have an index column, use a Set instead. This seems to put people off who assume that List should just be a more convenient way of accessing an unordered collection. Hibernate collections strictly obey the actual semantics attached to the Set, List and Map interfaces. List elements don't just spontaneously rearrange themselves.

On the other hand, people who planned to use the List to emulate "bag"-style semantics have a legitimate grievance here. Fortunately you can map a List or Collection with bag semantics.



My doubt is :

In my example, I have a parent "Program" and a child "Episode". One Program can have many episodes. I implemented them as a List.

In the mapping, I gave

<list name = "episodes" inverse="true" lazy="true" cascade="all">
<key column="PROGRAM_ID"/>
<index column="EPISODE_NO"/>
<one-to-many class="com.nielsenmedia.myEVNTS.Episode"/>
</list>

If you see my schema, I have use the Primary Key(which will be an index as well). Is this a correct implementation? Please advice.

Thanks
Vamsi


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 6:42 pm 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
If I try to persist the DistributorProgram object without any episodes like below, it is working just fine.

confused...more confused.


Code:
for (int counter = 0; counter < TOTAL_OBJECTS; counter++) {
        DistributorProgram program = new DistributorProgram("PROGRAM " +
            counter,
            121);

        /*for (int i = 1; i <= 10; i++) {
          Episode episode = new Episode("Episode"+i, i);
          episode.setDistributorProgram(program); // cross referencing many-to-one
          program.addEpisode(episode); // one-to-many
        }*/
        log.info("The number of episodes are " + program.getEpisodeCount() );
        hs.add(program);
        program = null;
      }




BTW, I did a simple performance test for plain inserts and I am very impressed. For 1 lakh records, using Plain JDBC insert it took 43 seconds, and using Hibernate I could do the same in 44 seconds. I am doing other tests and if we could get good numbers, we can convince our management that we can use Hibernate for atleast 70% of the object persistence.

Great Job Gavin and team. Thank you.

Vamsi


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 6:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
<list name = "episodes" inverse="true" lazy="true" cascade="all">
      <key column="PROGRAM_ID_FK"/>
      <index column="EPISODE_NO"/>   
      <one-to-many class="com.media.myEVNTS.Episode"/>
   </list>


key column refers to the column used by the association.
And in your schema, change
Code:
EPISODE_NO int NULL,
to
Code:
EPISODE_NO int NOT NULL,

since it is the list index.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:07 pm 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="com.media.myEVNTS.DistributorProgram" table="dist_program">

        <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->
   <id name="programID" type="string" unsaved-value="null" >
            <column name="PROGRAM_ID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>
      <!-- A program has to have a name, but it shouldn' be too long. -->
      <property name="programName">
           <column name="PROGRAM_NAME" sql-type="varchar(16)" not-null="true"/>
      </property>

      <property name="duration">
           <column name="PROGRAM_DURATION" sql-type="int" not-null="true"/>
      </property>
   <list name = "episodes" table="episode" inverse="true" lazy="true" cascade="all">
      <key column="PROGRAM_ID_FK"/>
      <index column="EPISODE_ID"/>   
      <one-to-many class="com.media.myEVNTS.Episode"/>
   </list>
    </class>

<class name="com.media.myEVNTS.Episode" table="episode" dynamic-update="true" dynamic-insert="true" >      
      <id name="episodeID" column="EPISODE_ID" type="string" unsaved-value="null">
         <column name="EPISODE_ID" sql-type="char(32)" not-null="true"/>
         <generator class="native"/>
      </id>      
      <property name="episodeName">
              <column name="EPISODE_NAME" sql-type="varchar(16)"/>
         </property>
      <property name="episodeNo">
              <column name="EPISODE_NO" sql-type="int"/>
         </property>
      <many-to-one name="distributorProgram" column="PROGRAM_ID" not-null="true"/>

</class>

</hibernate-mapping>



I changed the mapping file. But I am still getting this error. :(

Pl help me out..

Thanks
Vamsi


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:08 pm 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
My schema is now:

CREATE TABLE dbo.dist_program
(
PROGRAM_ID char(32) NOT NULL,
PROGRAM_NAME varchar(16) NULL,
PROGRAM_DURATION int NULL,
CONSTRAINT dist_progr_PROGRA_8299620331
PRIMARY KEY CLUSTERED (PROGRAM_ID)
)
LOCK ALLPAGES
go
IF OBJECT_ID('dbo.dist_program') IS NOT NULL
PRINT '<<< CREATED TABLE dbo.dist_program >>>'
ELSE
PRINT '<<< FAILED CREATING TABLE dbo.dist_program >>>'
go


CREATE TABLE dbo.episode
(
EPISODE_ID numeric(18,0) IDENTITY,
PROGRAM_ID_FK char(32) NOT NULL,
EPISODE_NAME varchar(16) NULL,
EPISODE_NO int NULL,
CONSTRAINT episode_8939622611
PRIMARY KEY CLUSTERED (EPISODE_ID)
)
LOCK ALLPAGES
go
IF OBJECT_ID('dbo.episode') IS NOT NULL
PRINT '<<< CREATED TABLE dbo.episode >>>'
ELSE
PRINT '<<< FAILED CREATING TABLE dbo.episode >>>'
go


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
<many-to-one name="distributorProgram" column="PROGRAM_ID_FK" not-null="true"/>

_________________
Emmanuel


Top
 Profile  
 
 Post subject: No improvement in the result....
PostPosted: Wed Nov 12, 2003 10:32 am 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
I changed the mapping file as suggested. But no improvement...

I am getting the error as follows:

Code:
09:05:50,868 ERROR [STDERR] net.sf.hibernate.MappingException: No persister for:
java.util.ArrayList
        at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryI
mpl.java:420)
        at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2302)

        at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2309)

        at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1195)

        at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:88)
        at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:258)
        at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:298)
        at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:341)
        at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:739)
        at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)


This is the mapping file...

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="com.nielsenmedia.myEVNTS.DistributorProgram" table="dist_program">

        <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->
   <id name="programID" type="string" unsaved-value="null" >
            <column name="PROGRAM_ID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>
      <!-- A program has to have a name, but it shouldn' be too long. -->
      <property name="programName">
           <column name="PROGRAM_NAME" sql-type="varchar(16)" not-null="true"/>
      </property>

      <property name="duration">
           <column name="PROGRAM_DURATION" sql-type="int" not-null="true"/>
      </property>
   <list name = "episodes" table="episode" inverse="true" lazy="true" cascade="all">
      <key column="PROGRAM_ID_FK"/>
      <index column="EPISODE_ID"/>   
      <one-to-many class="com.nielsenmedia.myEVNTS.Episode"/>
   </list>
    </class>

<class name="com.nielsenmedia.myEVNTS.Episode" table="episode" dynamic-update="true" dynamic-insert="true" >      
      <id name="episodeID" column="EPISODE_ID" type="string" unsaved-value="null">
         <column name="EPISODE_ID" sql-type="char(32)" not-null="true"/>
         <generator class="native"/>
      </id>      
      <property name="episodeName">
              <column name="EPISODE_NAME" sql-type="varchar(16)"/>
         </property>
      <property name="episodeNo">
              <column name="EPISODE_NO" sql-type="int"/>
         </property>
      <many-to-one name="distributorProgram" column="PROGRAM_ID_FK" not-null="true"/>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 12:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hum, can't figure it out why.

show me your code between openSession and close session.

And try to remove ArrayList from DistributorProgram class.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 12:30 pm 
Beginner
Beginner

Joined: Tue Nov 04, 2003 2:31 pm
Posts: 28
Location: Florida, US
Partial success!!!

I made the change to my Parent class as follows and the error
No persister for: java.util.ArrayList stopped coming.

Now I am getting a new error :

11:19:05,023 INFO [ProcessEchoEJB] The number of episodes are 10
11:19:05,226 ERROR [STDERR] net.sf.hibernate.HibernateException: Another object
was associated with this id (the object with the given id was already loaded): [
com.nielsenmedia.myEVNTS.Episode#0]
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1281)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1206)

at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:88)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:258)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:298)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:341)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:739)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)


My Parent class is

Code:
package com.media.myEVNTS;

import java.util.List;
import java.util.ArrayList;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class DistributorProgram {
  String programID;
  String programName;
  int duration;
  List episodes;

  public DistributorProgram() {
    //episodes = new ArrayList();
  }

  public DistributorProgram(String programID, String programName, int duration) {
    this();
    this.programID = programID;
    this.programName = programName;
    this.duration = duration;
  }

  public DistributorProgram(String programName, int duration) {
    this();
    this.programName = programName;
    this.duration = duration;
  }

  public void addEpisode(Episode episode) {
    episode.setDistributorProgram(this);
    episodes.add(episodes);
  }

  public void setEpisodes(List episodes){
    this.episodes=episodes;
  }

  public List getEpisodes(){
    return this.episodes;
  }

  public void removeEpisode(Episode episode) {
    //to be implemented

  }

  public int getEpisodeCount() {
    if (episodes != null) {
      return episodes.size();
    }
    return 0;
  }



  public void setProgramName(String programName) {
    this.programName = programName;
  }

  public String getProgramName() {
    return this.programName;
  }

  public void setProgramID(String programID) {
    this.programID = programID;
  }

  public String getProgramID() {
    return this.programID;
  }

  public void setDuration(int programID) {
    this.duration = duration;
  }

  public int getDuration() {
    return this.duration;
  }

}


This is my hibernate code:

Code:
private long testWithHibernate() throws Exception {
    long begin, stop;

    try {

      HashSet hs = new HashSet();
      for (int counter = 0; counter < TOTAL_OBJECTS; counter++) {
        DistributorProgram program = new DistributorProgram("PROGRAM " +
            counter,
            121);
        List episodes = new ArrayList();
        Episode episode;
        for (int i = 1; i <= 10; i++) {
          episode = new Episode("Episode" + i, i);
          episode.setDistributorProgram(program); // cross referencing many-to-one
          episodes.add(episode);
          episode = null;
        }
        program.setEpisodes(episodes); // one-to-many
        log.info("The number of episodes are " + program.getEpisodeCount());
        hs.add(program);
        program = null;
      }
      Iterator itr = hs.iterator();
      int counter = 0;
      begin = System.currentTimeMillis();
      openSession();
      while (itr.hasNext()) {
        DistributorProgram program = (DistributorProgram) itr.next();
        counter++;
        session.save(program);
        if (counter % 1000 == 0) {
          flushSession();
          Runtime.getRuntime().gc();
        }
        program = null;
      }
      //flushAndCloseSession();
      stop = System.currentTimeMillis();
      return (stop - begin);
    }
    catch (HibernateException hiberExcep) {
      hiberExcep.printStackTrace();
      log.info("HibernateException!!!");
      throw new RemoteException("HibernateException!!!");
    }
    catch (Exception excep) {
      excep.printStackTrace();
      throw excep;
    }
    finally {
      try {
        flushSession();
        closeSession();
      }
      catch (HibernateException hiberExcep) {
        log.info(
            "HibernateException while closing session - Jeopardy!!!");
        throw new Exception(
            "HibernateException while closing session - Jeopardy!!!");
      }
    }

  }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 35 posts ]  Go to page 1, 2, 3  Next

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.