-->
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.  [ 9 posts ] 
Author Message
 Post subject: Mapping
PostPosted: Wed Feb 04, 2004 11:32 am 
Newbie

Joined: Wed Feb 04, 2004 10:55 am
Posts: 5
I have a situation my tables are
1. Table A has fields id,tableB_id1,tableB_id2,name
2. Table B has fields id1,id2,Address
3. Table C has fields code,tableA_id,someInfo
Code:
TableA                  TableB            TableC
*ID        -----       *ID1              *code                 
tableB_id1 One to One    *ID2              *tableA_id         
tableB_id2                Address           someInfo
name   -----one to many ---------------------   

* - Key
My tableA has id as key, and my tableB has composite key id1,id2, and tableC does not have its own primarykey but has only reference key to tableA
I am new to hibernate can someone please guide me how to write mappings and reference in Java files, I already went through the documentation and I am confused can someone give me a example thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 12:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
quicky

ObjectC
many-to-one ObjectA

ObjectA
one-to-many Object C
many-to-one Object B

Object B
one-to-one property-ref=property of ObjectA used to link

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 1:07 pm 
Newbie

Joined: Wed Feb 04, 2004 10:55 am
Posts: 5
thanks a lot, but how do assign composite key for my tableA and how do i reference it back in xml for tableA.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 2:07 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Something like:
Code:
<many-to-one
  name="B"
  class="B">
  <column name="tableB_id1"/>
  <column name="tableB_id2"/>
</many-to-one>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 11:21 am 
Newbie

Joined: Wed Feb 04, 2004 10:55 am
Posts: 5
I tried it here are my files, can someone tell me what's wrong in my mapping Please.
Code:
/*
* Created on Feb 4, 2004
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
package org.jcboe.db.hb.hibernate;

import java.util.Set;
import java.util.HashSet;
/**
* @author singh
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class TableA {

   private int id;
   private int tableB_id1;
   private int tableB_id2;
   private String names;
   private TableB b;
   private Set tablec=new HashSet();
   /**
    * @return
    */
   public int getId() {
      return id;
   }

   /**
    * @return
    */
   public String getNames() {
      return names;
   }

   /**
    * @return
    */
   public int getTableB_id1() {
      return tableB_id1;
   }

   /**
    * @return
    */
   public int getTableB_id2() {
      return tableB_id2;
   }

   /**
    * @param i
    */
   public void setId(int i) {
      id = i;
   }

   /**
    * @param string
    */
   public void setNames(String string) {
      names = string;
   }

   /**
    * @param i
    */
   public void setTableB_id1(int i) {
      tableB_id1 = i;
   }

   /**
    * @param i
    */
   public void setTableB_id2(int i) {
      tableB_id2 = i;
   }

   /**
    * @return
    */
   public Set getTablec() {
      return tablec;
   }

   /**
    * @param set
    */
   public void setTablec(Set set) {
      tablec = set;
   }

   /**
    * @return
    */
   public TableB getB() {
      return b;
   }

   /**
    * @param tableB
    */
   public void setB(TableB tableB) {
      b = tableB;
   }

}

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="org.jcboe.db.hb.hibernate.TableA" table="tablea">
        <id name="id" type="int">
          <column name="id" sql-type="int"/>
            <generator class="increment"/>
        </id>
        <property name="names"/>
        <property name="tableB_id1"/>
        <property name="tableB_id2"/>
        <many-to-one name="b" class="TableB">
               <column name="id1"/>
               <column name="id2"/>
       </many-to-one>   
      <set name="tablec" table=tablec>
           <key column="tablea_id"/>
            <one-to-many class="TableC"/>
        </set>
    </class>

</hibernate-mapping>
<!-- parsed in 0ms -->

Code:
/*
* Created on Feb 4, 2004
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
package org.jcboe.db.hb.hibernate;

/**
* @author singh
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class TableB {
      private int id1;
      private int id2;
      private String address;
      private TableA btablea;
      /**
       * @return
       */
      public String getAddress() {
         return address;
      }

      /**
       * @return
       */
      public int getId1() {
         return id1;
      }

      /**
       * @return
       */
      public int getId2() {
         return id2;
      }

      /**
       * @param string
       */
      public void setAddress(String string) {
         address = string;
      }

      /**
       * @param i
       */
      public void setId1(int i) {
         id1 = i;
      }

      /**
       * @param i
       */
      public void setId2(int i) {
         id2 = i;
      }

      /**
       * @return
       */
      public TableA getBtablea() {
         return btablea;
      }

      /**
       * @param tableA
       */
      public void setBtablea(TableA tableA) {
         btablea = tableA;
      }

}

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="org.jcboe.db.hb.hibernate.TableB" table="tableb">
        <composite-id>
           <key-property name="id1"/>
           <key-property name="id2"/>
        </composite-id>
        <property name="address"/>
        <one-to-one property-ref="" name="btablea"/>
    </class>

</hibernate-mapping>
<!-- parsed in 0ms -->

Code:
/*
* Created on Feb 4, 2004
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
package org.jcboe.db.hb.hibernate;

/**
* @author singh
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class TableC {
      private String code;
      private int tableA_id;
      private String someInfo;
      private TableA ctablea;
      /**
       * @return
       */
      public String getCode() {
         return code;
      }

      /**
       * @return
       */
      public String getSomeInfo() {
         return someInfo;
      }

      /**
       * @return
       */
      public int getTableA_id() {
         return tableA_id;
      }

      /**
       * @param string
       */
      public void setCode(String string) {
         code = string;
      }

      /**
       * @param string
       */
      public void setSomeInfo(String string) {
         someInfo = string;
      }

      /**
       * @param i
       */
      public void setTableA_id(int i) {
         tableA_id = i;
      }

      /**
       * @return
       */
      public TableA getCtablea() {
         return ctablea;
      }

      /**
       * @param tableA
       */
      public void setCtablea(TableA tableA) {
         ctablea = tableA;
      }

}

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="org.jcboe.db.hb.hibernate.TableC" table="tablec">
        <property name="code"/>
        <property name="someInfo"/>
        <property name="tableA_id"/>
        <many-to-one name="ctablea"/>
    </class>

</hibernate-mapping>
<!-- parsed in 0ms -->


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 1:12 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Here's your problem:
Code:
<property name="tableB_id1"/>
<property name="tableB_id2"/>
<many-to-one name="b" class="TableB">
    <column name="id1"/>
    <column name="id2"/>
</many-to-one>

You're repeating the identifiers in the POJO. You don't need to do that. Having the many-to-one relationship takes care of mapping those identifiers in the table. Drop the tableB_id1 and tableB_id2 from the java object. Do the same thing in your TableC object.

Code:
<many-to-one name="b" class="org.jcboe.db.hb.hibernate.TableB">
    <column name="tableB_id1"/>
    <column name="tableB_id2"/>
</many-to-one>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 1:44 pm 
Newbie

Joined: Wed Feb 04, 2004 10:55 am
Posts: 5
this is the error i am getting
Code:
12:34:23,148  INFO Environment:432 - Hibernate 2.1.1
12:34:23,168  INFO Environment:466 - loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.filename=/WEB-INF/lib/mysql-connector-java-3.0.10-stable-bin.jar, hibernate.connection.password=, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.connection.url=jdbc:mysql://localhost/test, hibernate.connection.driver_class=com.mysql.jdbc.Driver}
12:34:23,178  INFO Environment:481 - using CGLIB reflection optimizer
12:34:23,178  INFO Configuration:318 - Mapping resource: org/jcboe/db/hb/hibernate/TableA.hbm.xml
12:34:23,408 ERROR XMLHelper:48 - Error parsing XML: XML InputStream(17) Value must be quoted.
12:34:23,408 ERROR Configuration:289 - Could not configure datastore from input stream
org.dom4j.DocumentException: Error on line 17 of document  : Value must be quoted. Nested exception: Value must be quoted.
   at org.dom4j.io.SAXReader.read(SAXReader.java:355)
   at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:281)
   at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:322)
   at org.jcboe.db.hb.InsertTest.main(InsertTest.java:29)
Nested exception:
org.xml.sax.SAXParseException: Value must be quoted.
   at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3182)
   at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3170)
   at org.apache.crimson.parser.Parser2.parseLiteral(Parser2.java:694)
   at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1399)
   at org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
   at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
   at org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
   at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
   at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:500)
   at org.apache.crimson.parser.Parser2.parse(Parser2.java:305)
   at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)
   at org.dom4j.io.SAXReader.read(SAXReader.java:339)
   at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:281)
   at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:322)
   at org.jcboe.db.hb.InsertTest.main(InsertTest.java:29)
net.sf.hibernate.MappingException: Error reading resource: org/jcboe/db/hb/hibernate/TableA.hbm.xml
   at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:325)
   at org.jcboe.db.hb.InsertTest.main(InsertTest.java:29)
Caused by: net.sf.hibernate.MappingException: org.dom4j.DocumentException: Error on line 17 of document  : Value must be quoted. Nested exception: Value must be quoted.
   at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:290)
   at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:322)
   ... 1 more
Caused by: org.dom4j.DocumentException: Error on line 17 of document  : Value must be quoted. Nested exception: Value must be quoted.
   at org.dom4j.io.SAXReader.read(SAXReader.java:355)
   at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:281)
   ... 2 more
Exception in thread "main" 


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 2:00 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Code:
Error on line 17 of document  : Value must be quoted. Nested exception: Value must be quoted.
   at org.dom4j.io.SAXReader.read(SAXReader.java:355)

And, did you look at line 17? Did you quote the value that must be quoted?

We can't debug your code for you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 3:21 pm 
Newbie

Joined: Wed Feb 04, 2004 10:55 am
Posts: 5
Ton of thanks for your help sir.


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