-->
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.  [ 8 posts ] 
Author Message
 Post subject: Associations, Collections problem. How to do this..
PostPosted: Thu Nov 10, 2005 2:40 pm 
Newbie

Joined: Thu Nov 10, 2005 2:21 pm
Posts: 6
Using:
Eclipse version: 3.1
Hibernate version: 3.1
Hibernate Synchronizer version: 3.1.1

I have mappings like this:

Assinantes:
Code:
<class
      name="Assinante"
      table="assinantes"     
   >
      <id
         name="Id"
         type="long"
         column="CODIGO"
      >
         <generator class="increment"/>
      </id>

      <property
         name="Email"
         column="EMAIL"
         type="string"
         not-null="true"
         length="50"
      />
     
      <set name="assinaturas"                   
          inverse="true"
          cascade="all-delete-orphan">         
         <key column="CODIGO_ASSINANTE"/>         
         <one-to-many class="Assinatura" />
      </set>
                   </class>


Assinaturas
Code:
<class
      name="Assinatura"
      table="assinaturas"
   >
      <id
         name="Id"
         type="long"
         column="CODIGO"
      >
         <generator class="increment"/>
      </id>
     
      <property
         name="EdicaoInicial"
         column="EDICAO_INICIAL"
         type="integer"
         not-null="true"
         length="11"
      />
      <property
         name="EdicaoFinal"
         column="EDICAO_FINAL"
         type="integer"
         not-null="true"
         length="11"
      />
     
      <many-to-one
         name="assinante"
         column="CODIGO_ASSINANTE"
         class="Assinante"                       
      />
   </class>


My tables:
Code:
Assinantes
+--------+------------------+------+-----+---------+-------+
| Field  | Type             | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+-------+
| CODIGO | int(20) unsigned |      | PRI | 0       |       |
| EMAIL  | varchar(50)      |      | MUL |         |       |
+--------+------------------+------+-----+---------+-------+

Assinaturas
+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| CODIGO           | int(10) unsigned |      | PRI | 0       |       |
| CODIGO_ASSINANTE | int(10) unsigned |      | MUL | 0       |       |
| EDICAO_INICIAL   | int(11)          |      |     | 0       |       |
| EDICAO_FINAL     | int(11)          |      |     | 0       |       |
+------------------+------------------+------+-----+---------+-------+


The question is, how can i get the set assinaturas into the object Assinante? When i do this:
Code:
Assinante assinante = session.get(Assinante.class, new Long(2));

It just loads the properties from the Assinantes table, not the set from the Assinaturas table, then if i do:
Code:
Set assinaturas = assinante.getAssinaturas();

it returns null.

When i add a 'assinatura' to the 'assinante' like this:
Code:
Assinatura assinatura = new Assinatura();
assinatura.setEmail("email@blabla");
assinante.getAssinaturas.add(assinatura);

it inserts in the table right, i just cant get the set..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 6:53 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Why have you got inverse="true"? That's your problem in this case. WIth inverse="true" on the set in Assinante, that means that adding things to the java Set won't cause any changes in the DB: you need to add the Assinante instance to every object in the assinatura set. And you're not doing that.

To make your existing code work, move the inverse="true" to the many-to-one link in the Assinatura mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 9:04 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is no inverse="true" on many-to-one. You both should read the Parent/Child example chapter in the docs again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 9:09 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Shame on me. I'm always recollecting when I should be researching :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 7:26 am 
Newbie

Joined: Thu Nov 10, 2005 2:21 pm
Posts: 6
But christian, i've read that and i dont see what am i doing wrong.
Is it possible that the classes generated by synchronizing for these mapping are wrong?
Here are them:

Base class for Assinante
Code:
public abstract class BaseAssinante  implements Comparable, Serializable {

   public static String REF = "Assinante";
   public static String PROP_EMAIL = "Email";
   public static String PROP_ID = "Id";


   // constructors
   public BaseAssinante () {
      initialize();
   }

   /**
    * Constructor for primary key
    */
   public BaseAssinante (long id) {
      this.setId(id);
      initialize();
   }

   /**
    * Constructor for required fields
    */
   public BaseAssinante (
      long id,
      java.lang.String email) {

      this.setId(id);
      this.setEmail(email);
      initialize();
   }

   protected void initialize () {}



   private int hashCode = Integer.MIN_VALUE;

   // primary key
   private long id;

   // fields
   private java.lang.String email;

   // collections
   private java.util.Set<wsol.centaurus.model.bean.Assinatura> assinaturas;



   /**
    * Return the unique identifier of this class
     * @hibernate.id
     *  generator-class="increment"
     *  column="CODIGO"
     */
   public long getId () {
      return id;
   }

   /**
    * Set the unique identifier of this class
    * @param id the new ID
    */
   public void setId (long id) {
      this.id = id;
      this.hashCode = Integer.MIN_VALUE;
   }




   /**
    * Return the value associated with the column: EMAIL
    */
   public java.lang.String getEmail () {
      return email;
   }

   /**
    * Set the value related to the column: EMAIL
    * @param email the EMAIL value
    */
   public void setEmail (java.lang.String email) {
      this.email = email;
   }



   /**
    * Return the value associated with the column: assinaturas
    */
   public java.util.Set<wsol.centaurus.model.bean.Assinatura> getAssinaturas () {
      return assinaturas;
   }

   /**
    * Set the value related to the column: assinaturas
    * @param assinaturas the assinaturas value
    */
   public void setAssinaturas (java.util.Set<wsol.centaurus.model.bean.Assinatura> assinaturas) {
      this.assinaturas = assinaturas;
   }

   public void addToassinaturas (wsol.centaurus.model.bean.Assinatura assinatura) {
      if (null == getAssinaturas()) setAssinaturas(new java.util.TreeSet<wsol.centaurus.model.bean.Assinatura>());
      getAssinaturas().add(assinatura);
   }





   public boolean equals (Object obj) {
      if (null == obj) return false;
      if (!(obj instanceof wsol.centaurus.model.bean.Assinante)) return false;
      else {
         wsol.centaurus.model.bean.Assinante assinante = (wsol.centaurus.model.bean.Assinante) obj;
         return (this.getId() == assinante.getId());
      }
   }

   public int hashCode () {
      if (Integer.MIN_VALUE == this.hashCode) {
         return (int) this.getId();
      }
      return this.hashCode;
   }

   public int compareTo (Object obj) {
      if (obj.hashCode() > hashCode()) return 1;
      else if (obj.hashCode() < hashCode()) return -1;
      else return 0;
   }

   public String toString () {
      return super.toString();
   }


Base class for Assinatura:
Code:
public abstract class BaseAssinatura  implements Comparable, Serializable {

   public static String REF = "Assinatura";
   public static String PROP_EDICAO_FINAL = "EdicaoFinal";
   public static String PROP_ASSINANTE = "assinante";
   public static String PROP_EDICAO_INICIAL = "EdicaoInicial";
   public static String PROP_ID = "Id";


   // constructors
   public BaseAssinatura () {
      initialize();
   }

   /**
    * Constructor for primary key
    */
   public BaseAssinatura (long id) {
      this.setId(id);
      initialize();
   }

   /**
    * Constructor for required fields
    */
   public BaseAssinatura (
      long id,
      wsol.centaurus.model.bean.Assinante assinante,
      java.lang.Integer edicaoInicial,
      java.lang.Integer edicaoFinal) {

      this.setId(id);
      this.setAssinante(assinante);
      this.setEdicaoInicial(edicaoInicial);
      this.setEdicaoFinal(edicaoFinal);
      initialize();
   }

   protected void initialize () {}



   private int hashCode = Integer.MIN_VALUE;

   // primary key
   private long id;

   // fields
   private java.lang.Integer edicaoInicial;
   private java.lang.Integer edicaoFinal;

   // many to one
   private wsol.centaurus.model.bean.Assinante assinante;



   /**
    * Return the unique identifier of this class
     * @hibernate.id
     *  generator-class="increment"
     *  column="CODIGO"
     */
   public long getId () {
      return id;
   }

   /**
    * Set the unique identifier of this class
    * @param id the new ID
    */
   public void setId (long id) {
      this.id = id;
      this.hashCode = Integer.MIN_VALUE;
   }




   /**
    * Return the value associated with the column: EDICAO_INICIAL
    */
   public java.lang.Integer getEdicaoInicial () {
      return edicaoInicial;
   }

   /**
    * Set the value related to the column: EDICAO_INICIAL
    * @param edicaoInicial the EDICAO_INICIAL value
    */
   public void setEdicaoInicial (java.lang.Integer edicaoInicial) {
      this.edicaoInicial = edicaoInicial;
   }



   /**
    * Return the value associated with the column: EDICAO_FINAL
    */
   public java.lang.Integer getEdicaoFinal () {
      return edicaoFinal;
   }

   /**
    * Set the value related to the column: EDICAO_FINAL
    * @param edicaoFinal the EDICAO_FINAL value
    */
   public void setEdicaoFinal (java.lang.Integer edicaoFinal) {
      this.edicaoFinal = edicaoFinal;
   }



   /**
    * Return the value associated with the column: CODIGO_ASSINANTE
    */
   public wsol.centaurus.model.bean.Assinante getAssinante () {
      return assinante;
   }

   /**
    * Set the value related to the column: CODIGO_ASSINANTE
    * @param assinante the CODIGO_ASSINANTE value
    */
   public void setAssinante (wsol.centaurus.model.bean.Assinante assinante) {
      this.assinante = assinante;
   }





   public boolean equals (Object obj) {
      if (null == obj) return false;
      if (!(obj instanceof wsol.centaurus.model.bean.Assinatura)) return false;
      else {
         wsol.centaurus.model.bean.Assinatura assinatura = (wsol.centaurus.model.bean.Assinatura) obj;
         return (this.getId() == assinatura.getId());
      }
   }

   public int hashCode () {
      if (Integer.MIN_VALUE == this.hashCode) {
         return (int) this.getId();
      }
      return this.hashCode;
   }

   public int compareTo (Object obj) {
      if (obj.hashCode() > hashCode()) return 1;
      else if (obj.hashCode() < hashCode()) return -1;
      else return 0;
   }

   public String toString () {
      return super.toString();
   }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 9:15 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Lazy Loading is the issue. In Hibernate 3.x, Lazy loading is enabled by default and since by default its enabled, when hibernate fetches the record, it fetches only that and not its associations.
Enable lazy=true on the set in your Set mapping and you should get what you want.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 9:58 am 
Newbie

Joined: Thu Nov 10, 2005 2:21 pm
Posts: 6
if you say that its enabled by default if write lazy="true" it wont make any difference.

I tried with lazy="true", lazy="false", outer-join="true", everything..
i doesnt seem to work..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 11:19 am 
Newbie

Joined: Thu Nov 10, 2005 2:21 pm
Posts: 6
i managed to solve the problem guys, it was a stupid mistake in the database, but difficult for me to realize..
the id's CODIGO, CODIGO_ASSINANTE had not the same type, i changed its type from int to bigint and it worked fine as the type in the mapping files were long..

thank you anyway,
Juliano.


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