-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to map a primary key that's not in the java classes?
PostPosted: Fri Sep 09, 2005 7:44 am 
Newbie

Joined: Fri Sep 09, 2005 4:21 am
Posts: 5
Hello everybody,

I've searched through the forum and some books but i can not find any answers to my question.

My problem is about many-to-many relationships which the relationship table has another primary key (other than the foreign keys) and if this primary key can not be mapped from java classes. (It's a key that's automatically given depending on the composite primary keys) Let me give an example:

_______________
|PERSON
|---------------------
|PERSON_ID (PK)
|NAME
|SURNAME
|_______________

_________________
|COMPANY
|-----------------------
|COMPANY_ID (PK)
|NAME
|ADDRESS_TEXT
|_________________

_________________
|COMPANY-PERSON
|------------------------
|COMPANY_ID (PK)
|PERSON_ID (PK)
|INDEX (PK)
|SALARY
|_________________

INDEX is just an integer that is incrementally given to a specific (COMPANY_ID,PERSON_ID) pair. SALARY, is the current salary of the person. Note that a person's salary can change within the time, so there can be more than one relationship in the same table (between person and company) so that's why index is the other primary key. (i don't want to update the salary because i am logging the history)

i have no problem with mapping many-to-many relationships to the database. But here is the case: Think about the java classes,

POJO used

/**************************************************

Person.java

public class Person {

private int id;
private String name;
private String surname;

//setters and getters
}

/****************************************************

/**************************************************

Company.java

public class Company {

private int id;
private String name;
private String address;

//setters and getters
}

/****************************************************

CompanyPerson.java

public class CompanyPerson {

private Person person;
private Company company;
private String salary;

//setters and getters
}

As you can see, index isn't in the CompanyPerson.java class. (and i don't want to put it there). So i can not map any variable to INDEX attribute of the COMPANY-PERSON table. Normally by SQL, i can easily insert it by the query :

" INSERT INTO COMPANY-PERSON (COMPANY_ID, PERSON_ID, INDEX,SALARY) VALUES (?,?, (select nvl(max(INDEX),0) from COMPANY-PERSON where COMPANY_ID=? and PERSON_ID = ?)+1,?)"

Is it possible to make it with hibernate and if it's possible how?

Hibernate version:
3.0

Name and version of the database
Oracle 10 G

Thank you for your answers..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 8:53 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Can you create empty setters and getters for this "field"?
And leaving out the access attribute like in:

Code:
        <property
            name="doubleVal"
            type="double"
            column="dVal"
        />


should use the default access strategy:

Quote:
The access attribute lets you control how Hibernate will access the property at runtime. By default, Hibernate will call the property get/set pair.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 10:26 am 
Newbie

Joined: Fri Sep 09, 2005 4:21 am
Posts: 5
Could you please explain a little more? I haven't used access attributes and can not find any example about its usage. After creating empty getters and setters (in source code) how should i map it in my own way?

Also can i do it using the formula something like that :

<hibernate-mapping>
<class name="CompanyPerson" table="COMPANY-PERSON">

......

<id name="index" column="INDEX" unsaved-value="0">
formula="(select nvl(max(INDEX),0) from COMPANY-PERSON where COMPANY_ID=? and PERSON_ID = ?)+1 />
</id>

......

</class>
</hibernate-mapping>

Thank you for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 10:34 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Hi,

in your case if the invisible field "index" is part of the primary key my
last suggestion will not work, since the hibernate session cache needs
the all primary key fields. What about an id class as
explained in section:

9.4. Components as composite identifiers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 3:34 am 
Newbie

Joined: Fri Sep 09, 2005 4:21 am
Posts: 5
Sorry for my late response, i had lots of work to do at the weekend so i could not use the internet.

Thank you for your help but i have another question. composite identifier may solve my question about mapping but how can i define my formula for the index attribute?


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