Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Join One to one

  1. #1

    Default Join One to one

    Iam having problem in joining two tables in hibernate using hbm.xml file.

    When Iam joining result are appearing but the values are wrong for eg:
    I have a table abc and xyz in the database
    abc contains three

    column
    id userid date
    1 1 20/01/2007
    2 1 17/01/2007
    3 2 20/01/2007
    4 1 18/01/2007
    5 3 20/01/2007

    xyz contains three column

    userid name
    1 Liju
    2 Thomas
    following is the code i have written:

    Code:
    <hibernate-mapping>
    	<class name="com.abc" table="xyz" dynamic-insert="false" dynamic-update="false" >
    	        <id name="userid" column="USERID" type="long">
    		<generator class="sequence">
    			<param name="sequence">ABC_SEQ</param>
    		</generator>
    		</id>
    		<property name="name" column="NAME" />
        <join table="ABC"  >
          <key>
            <column name="USERID" not-null="true"/>
          </key>
    	  <property name="id" column="ID" type="long" />
      	  <property name="date" column="DATE" type="java.util.Date" />
        </join>
     </class>
    </hibernate-mapping>
    Following is result Iam getting:

    1 Liju 20/01/2007
    2 Liju 20/01/2007
    3 Thomas 20/01/2007
    4 Liju 20/01/2007

    Which is wrong (Look Dates which are same)

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    You've said it's a one-to-one join, but it seems like it's one-to-many. Also when you say you're getting the wrong results, what for? What are you doing?

  3. #3

    Default

    Yes the results are wrong

    1 Liju 20/01/2007
    2 Liju 20/01/2007
    3 Thomas 20/01/2007
    4 Liju 20/01/2007

    same dates are repeating again....the results should be:

    1 Liju 20/01/2007
    2 Liju 17/01/2007
    3 Thomas 20/01/2007
    4 Liju 18/01/2007

    How to specify one to one relation ?

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    You didn't answer the question. What results are wrong? What are you doing? Are you retrieving the user and then looking at the associated objects? I don't see how the association can be one-to-one. Your data is one-to-many!

    UserId=1 there are two entries here, how can this be one-to-one?
    id userid date
    1 1 20/01/2007
    2 1 17/01/2007

  5. #5

    Default

    Answer to the Question:
    Following are the results

    1 Liju 20/01/2007
    2 Liju 20/01/2007
    3 Thomas 20/01/2007
    4 Liju 20/01/2007

    What results are wrong?

    same dates are repeating again....the results should be:

    1 Liju 20/01/2007
    2 Liju 17/01/2007
    3 Thomas 20/01/2007
    4 Liju 18/01/2007

    What are you doing

    I dont want the date to repeat...the date should be same as in the db

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Ok, one more try. I must be making a real hash of asking for information. What I'm trying to understand is what you are doing to see these results that are wrong. Are you querying for a User and then looking at the associated data and seeing that the user has the wrong values?

    As I said before the association isn't one-to-one, it's one-to-many. Have a look at this example, it's doing the same thing that you want to.
    http://www.hibernate.org/hib_docs/v3...irectional-12m

  7. #7
    Join Date
    Jun 2006
    Posts
    18

    Default

    You have to define the correct association for these two table. It should work fine.

  8. #8
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Did you manage to get this working? The example in the Hibernate documentation should have put you on the right track.

  9. #9

    Default

    Quote Originally Posted by karldmoore View Post
    Did you manage to get this working? The example in the Hibernate documentation should have put you on the right track.
    In the documentation there are two classes in my case there is one class but internally iam joing two tables


    How will i specify one to one relationship

  10. #10

    Default

    you have the key column on the table you are joining to as the userid which is not unique as each of the rows returned has the same one - change this to be the id (or something unique) and it should retrieve the correct data. Or use a composite id column - using userid and id

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •