How bidirectional one-to-one association on a foreign key [duplicate] - java

This question already has answers here:
Hibernate - why use many-to-one to represent a one-to-one?
(5 answers)
Closed 7 years ago.
Reading some hibernate documentation, I've stumbled upon this pattern :
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class>
from jboss hibernate doc
At first glance this seems pretty straightforward. Each Person have one and only one address, and each Address have a person.
But in a DB this pattern would allow an Address to have multiple Person referencing it, how hibernate could resolve this mapping without a list ?
How a one-to-one can have a property-ref on a many-to-one field ? I think it should only be possible to have many-to-one <=> one-to-many or one-to-one <=> one-to-one.
edit : Althought it seems possible (if not semantically correct) to make a one-to-one <=> one-to-many mapping, my question is about many-to-one <=> one-to-one.
I know that there are already some answers about one-to-one vs many-to-one on this site, but I didn't found an answer to my question in these posts.
Thank you for your time.

If you have the unique constraint in the DB, then it would not be possible that two persons have the same address.
However, if the constraint is missing and there really are multiple persons with the same address, then this mapping is not correct and you may get an exception or wrong/unpredictable results when accessing the person association in the Address entity.

Related

Why LEFT OUTER Join is needed for one-to-one bidirectional association

I am following Hibernate documentation for bidirectional one-to-one relationship and created one-to-one mapping between Person and Address entities.
The mapping is like the one given in document:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class>
Now I created s smll program to fetch the Person and the corresponding Address like this:
List<Person> persons = session.createQuery("from Person where id=2").list();
for (Person person : persons) {
System.out.println(person);
System.out.println(person.getAddress());
System.out.println(person.getAddress().getPerson());
}
For the line person.getAddress(), hibernate issues a LEFT OUTER JOIN query as:
select
address0_.addressId as addressId1_0_0_,
person1_.personId as personId1_1_1_,
person1_.addressId as addressId3_1_1_
from
ADDRESS address0_
left outer join
PERSON person1_
on address0_.addressId=person1_.addressId
where
address0_.addressId=?
If a inner join is sufficient in this case then why hibernate uses left outer join? Please clarify.
Hibernate is loading the address using it's id (which it got from the PERSON). During the loading it is not sure whether the address has a person (it does not remember where it got the id from).
If the address had no person, and it used an inner join, no results would be returned.
It uses outer join, so that if there is no person, a result will be returned with NULL in columns of the person.
This is what hibernate wants, so it uses outer join.

hibernate one to one on foreign key

I have a main table that is D1 which has a unique id of d1Id. d1Id uniquely identifies the D1 records and is also a primary key on the table. I have table 2 which is D2 which has a d2seq as a primary key but D2 also has d1Id which is unique which has a foreign key constraint (I think) on D1. But I know for sure it's unique and the id's are the same. I am trying to retrieve d2 values when i make the D1 call using hibernate and i'm unable to get the right hbm for it as i'm getting compile time errors.
The bottom line of the issue is, I need to establish a one to one relationship between 2 tables where in the second table, the id which joins is not the primary key of the table. All the examples i've seen searching google have the case where the ID on the second table is also the primary key of the second table. Let me show you the HBM file I have right now.
<hibernate-mapping default-lazy="true"
package="com.xxx.xx.customer.dto">
<class name="D1" table="D1" dynamic-update="true">
<id name="dString" column="DID" type="java.lang.String"
unsaved-value="null" length="9">
<generator class="assigned"/>
</id>
...
<one-to-one name="d2" class="com.xxx.xx.customer.dto.D2" >
<!-- column name="d1Id" /-->
</one-to-one>
In my second hbm for D2, i'm stuck here
<hibernate-mapping default-lazy="true" package="com.xxx.xx.customer.dto">
<class name="D2" table="D2" dynamic-update="true">
<id name="id" column="D2ID" type="integer">
<generator class="native">
<param name="sequence">D2SEQ</param>
</generator>
</id>
<id name="d1id" column="D1ID" type="java.lang.String"
unsaved-value="null" length="9">
<generator class="assigned"/>
</id>
I obviously can't use the second id field for d1id here because 1> it's not a primary key and 2> i've already used it before. So how should my HBM files be for this operation?
Also once I have my HBMs in place, I would like to get D2 information while i query D1 through the DTOs. How can I do that?
Why can't you use #ManyToOne relationship? Often it provides more flexibility than #OneToOne

Why using a #Many-to-one in JPA entity relationships?

My question might look silly. I am new to JPA and trying to understand the underlying concepts of it. I found out that there is a #Many-to-one entity relationship that can be used there. My question is why someone would want to use this while have #One-to-many relationship?
I mean, having the latter one is enough to know the relationship and sending queries right?
if not please explain. Maybe the idea that I have about these two relationships are completely wrong. Please provide a scenario as an example so I understand better.
Thanks
OneToMany is used to map collections. If you want this relation to be bidirectional then you need to add a ManyToOne mapping on the other side.
With Hibernate mapping : Person N <---> 1 Address
<class name="Person">
<id name="id" column="personId"/>
<many-to-one name="address" column="addressId"/>
</class>
<class name="Address">
<id name="id" column="addressId"/>
<set name="people">
<key column="addressId"/>
<one-to-many class="Person"/>
</set>
</class>
ManyToOne can also be used to map a dependency between 2 objects. In this case if you want the relation to be bidirectional then you need to add a OneToOne mapping on the other side.
With Hibernate mapping : Person 1 <---> 1 Address
<class name="Person">
<id name="id" column="personId"/>
<many-to-one name="address" column="addressId" unique="true"/>
</class>
<class name="Address">
<id name="id" column="addressId"/>
<one-to-one name="person" property-ref="address"/>
</class>
#ManyToOne and #OneToManyare both part of the same concept used to describe the two sides of a single bidirectional relationship.
If you want the relationship to be unidirectional - i.e. one of the entities cannot traverse to the other, you can use one of the annotations. To decide which one, you will have to decide which entity should know about the relation and which should not.
But often you want a bidirectional relation - then you use both.

Hibernate mapping with multiple but separate Primary Keys (OR)

I am currently working with hibernate and have already tried for a couple of weeks to find a solution for the following problem. As always a change of the tables is not possible.
There are two tables like this:
Table 1
-> ID
-> REF_ID
-
Table 2
-> ID
ID is the primary key for each table. REF_ID is mostly 0, but sometimes contains valid IDs.
So I am looking to realize something like that in hibernate:
select
....
from
Table1 this_
inner join
Table2 this_1_
on this_.ID=this_1_.ID
or this_.REF_ID=this_1_.ID
I have to apply certain criterias in the java code so I probably cannot use HQL. Also I am already using the code for other tables, so ideally I would like to use mapping files. My current mapping files look like this:
<hibernate-mapping>
<class name="Table1" table="Table1">
<id name="ID" type="string">
<column name="ID" />
</id>
<join table = "Table2" fetch = "join">
<key column ="ID"/>
</join>
</class>
</hibernate-mapping>
I hope someone can suggest a good approach.

Lazy One-to-One using Proxy not working

I've got a one-to-one relation between a dealer and a seller which should be lazy using a proxy. For the side on which the foreign key is defined (the seller, references the dealer), this works fine. But it doesn't work from the other side - the seller is always loaded eagerly. I set constrained="true" as described in "Some explanations on lazy loading", but that didn't help.
Following is the used mapping:
<class name="Role" table="PER_PERSROLE" abstract="true">
<id column="OID" type="long">
<generator class="native" />
</id>
<discriminator column="SUBTYPE" type="string" />
</class>
<subclass name="Dealer" extends="Role" discriminator-value="DEAL">
<property name="gpNr" column="GP_NR" type="string" />
<one-to-one name="seller" property-ref="dealer" lazy="proxy" constrained="true"
outer-join="false" />
</subclass>
<subclass name="Seller" extends="Role" discriminator-value="SELL">
<many-to-one name="dealer" column="SELLER_DEALEROID" lazy="proxy"
outer-join="false" />
</subclass>
Is it a problem that both classes reside in one table? I see that strictly speaking the relation isn't constrained on the database (it can't using this model), but the domain model always needs both entities and the application ensures this.
I think that the page you linked to explained it best, though I am not sure why it recommends setting constrained="true". If you think about it on the database level, Hibernate can't tell if the given property (the Dealer's seller) should be null or not without hitting the database (it needs to do a SELECT ... WHERE OID=:sellerOrDealerId to see if any rows are returned). And while it's hitting the database, it might as well fetch the rest of the row. From the other side of the association (Seller's dealer), there are no such problems since it has already fetched the row (and thus the SELLER_DEALEROID column).
I did encounter something similar to this once and was able to solve it by making the association non-optional (err, non-nullable)
I'm guessing you are loading session.get(id)? Have you tried load the instance using an HQL query? That will allow you to specify in the query which relationships to eager or lazy load.
HTH

Categories

Resources