Establish relationship from one table to another table in Hibernate - java

I have 2 tables, one table is
City Table ( int id, string name)
and my another table is
Distance table(int id,int cityId (FK city),int neighbourId(FK city))
I want to use Hibernate but I can't establish a relationship between these tables in Hibernate.

what about something like
<class name="City" table="CITIES">
<id name="id" type="integer">
<generator class="native" />
</id>
<property name="name" />
<set name="neighbours" table="DISTANCES">
<key column="city_id" />
<many-to-one name="neighbour" class="City" />
</set>
</class>
didn't test it though.

Ok, I can see any problem to do it normally.
<class name="City" table="CITY">
<id name="id" type="integer">
<generator class="native" />
</id>
<property name="name" />
</class>
<class name="Distance" table="DISTANCE">
<id name="id" type="integer">
<generator class="native" />
</id>
<many-to-one name="city" column="cityId" class="City"/>
<many-to-one name="neighbour" column="neighbourId" class="City"/>
</class>
didn't test it neither.

Related

Hibernate One-to-One foreign key using xml Not deleting orphan

I have two classes A and B.
<id name="id" type="java.lang.Integer" column="A_ID">
<generator class="identity" />
</id>
<property name="name" column="NAME"/>
<many-to-one name="B" cascade="all"
column="B_ID" unique="true"/>
</class>
<id name="id" type="java.lang.Integer" column="B">
<generator class="identity" />
</id>
<property name="n" column="N"/>
</class>
A.setB(new B());
When I save it works fine.
when I remove the record. A.setB(null);
I see a orphan record.
I see in A Table the column for B is null but on B Table there is still a record.
I tried to look in other solutions but couldnot find the ans to this.

Hibernate one-to-one bidirectional mapping XML

I have 3 tables represented by the following diagram:
I am trying to build a REST API that when /template is accessed by GET method,
the returned JSON will contain a templateDefinition object, that also contains a template type object. To that end I have created the following mapping files:
Template.hbm.xml
<hibernate-mapping package="com.reporttemplateengine.models">
<class name="Template" table="TEMPLATE">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" column="NAME" type="string" />
<one-to-one name="templateDefinition" class="com.reporttemplateengine.models.TemplateDefinition" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
TemplateType.hbm.xml
<hibernate-mapping package="com.reporttemplateengine.models">
<class name="TemplateType" table="TEMPLATETYPE">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" column="NAME" type="string" />
</class>
</hibernate-mapping>
TemplateDefinition.hbm.xml
<hibernate-mapping package="com.reporttemplateengine.models">
<class name="TemplateDefinition" table="TEMPLATEDEFINITION">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<property name="templateFile" column="TEMPLATE_FILE" type="blob" />
<property name="version" column="VERSION" type="int" />
<property name="active" column="ACTIVE" type="int" />
<many-to-one name="template" class="com.reporttemplateengine.models.Template"
column="template_id" unique="true" not-null="true" cascade="all" />
<many-to-one name="templateType" class="com.reporttemplateengine.models.TemplateType"
column="template_type_id" unique="true" not-null="true" cascade="all" />
</class>
</hibernate-mapping>
When I call the API, Hibernate logs this query:
select
this_.ID as ID1_0_1_,
this_.NAME as NAME2_0_1_,
templatede2_.ID as ID1_1_0_,
templatede2_.TEMPLATE_FILE as TEMPLATE_FILE2_1_0_,
templatede2_.VERSION as VERSION3_1_0_,
templatede2_.ACTIVE as ACTIVE4_1_0_,
templatede2_.TEMPLATE_ID as TEMPLATE_ID5_1_0_,
templatede2_.TEMPLATE_TYPE_ID as TEMPLATE_TYPE_ID6_1_0_
from
SYSTEM.TEMPLATE this_
left outer join
SYSTEM.TEMPLATEDEFINITION templatede2_
on this_.ID=templatede2_.ID
It should actually look like this:
select
this_.ID as ID1_0_1_,
this_.NAME as NAME2_0_1_,
templatede2_.ID as ID1_1_0_,
templatede2_.TEMPLATE_FILE as TEMPLATE_FILE2_1_0_,
templatede2_.VERSION as VERSION3_1_0_,
templatede2_.ACTIVE as ACTIVE4_1_0_,
templatede2_.TEMPLATE_ID as TEMPLATE_ID5_1_0_,
templatede2_.TEMPLATE_TYPE_ID as TEMPLATE_TYPE_ID6_1_0_
from
SYSTEM.TEMPLATE this_
inner join
SYSTEM.TEMPLATEDEFINITION templatede2_
on this_.ID=templatede2_.TEMPLATE_ID
This is also missing the template type object nested in templateDefinition. I do not know how to achieve this.

Hibernate: Mapping with one-to-many on non-primary-key columns

I'm stuck at the hibernate xml mapping configuration.
I've established some tables with foreign key constraints in my MSSQL database:
Table ItemsBase
ID int primary-key
ItemID int unique index
... some more columns
Table Others
ID int primary-key
ItemID int unique index
... some more columns
The foreign key constraint is configured to connect these two tables by using the column "ItemID".
My ItemsBase.hbm.xml files looks like:
<hibernate-mapping>
<class name="de.delife.sql.ItemsBase" table="ItemsBase" schema="dbo" catalog="Delife_Plenty">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="itemId" type="java.lang.Integer">
<column name="ItemID" unique="true" />
</property>
<set name="otherses" table="Others" inverse="true" lazy="true" fetch="select">
<key property-ref="itemId">
<column name="ItemID" />
</key>
<one-to-many class="de.delife.sql.Others" not-found="ignore" />
</set>
</class>
</hibernate-mapping>
and the Others.hbm.xml files looks like:
<hibernate-mapping>
<class name="de.delife.sql.Others" table="Others" schema="dbo" catalog="Delife_Plenty">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="itemsBase" class="de.delife.sql.ItemsBase" fetch="select" property-ref="itemId">
<column name="ItemID" unique="true" />
</many-to-one>
</class>
</hibernate-mapping>
Everything looks fine for me, but when I run my program I get a hibernate error:
property [itemId] not found on entity [de.delife.sql.Others]
I have an established relation between ItemsBase and a table named ItemsProperties and it works, but with this "pretty" table "Others" I'm stuck.
I would be glad, if someone can help me on this matter. Thx in advance.
Try like this. I tried and its working for me.
<set name="otherses" table="Others" inverse="true" lazy="true" fetch="select">
<key column="itemId" foreign-key="itemId" property-ref="itemId"/>
<one-to-many class="de.delife.sql.Others" not-found="ignore" />
</set>
Item id should be defined as a property in Others.hbm.xml:
<property name="itemId" column="ItemId"/>

Regarding one to one mapping in hibernate

I am a new bie to the world of hibernate could you please advise , I was going through one to one relationship in Hibernate,
As per my analysis , one to one hibernate relationships can be established by three ways..
1)Through Join concept
2)Same primary key in both the tables
3)Primary key and foriegn key relationship in both the tables
Please advise , the above three ways to achieve the one to one mapping is correct or I am missing something then please advise, and also please advise that the below hbm mapping files that I am using are correct one.if not then please advise.
1) Through Join concept :-
One way to achieve to one to one relationship is by joining concept , following xml is being used for that
<hibernate-mapping>
<class name="mypack.Person" table="person21">
<id name="personId" type="int">
<generator class="increment">
</generator>
</id>
<property name="name"/>
<join table="personAddress">
<key column="personId"/>
<many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/>
</join>
</class>
<class name="mypack.Address" table="address21">
<id name="id" column="addressId" type="int">
<generator class="increment"/>
</id>
<property name="city"/>
<property name="state"/>
</class>
</hibernate-mapping>
2) Same primary key in both the tables :-
Using same primary key in both the tables , following hbm is being used for that
<class name="mypack.Address" table="address31">
<id name="id" column="addressId" type="int">
<generator class="increment"/>
</id>
<property name="city"/>
<property name="state"/>
</class>
<class name="mypack.Person" table="person31">
<id name="personId" type="int">
<generator class="foreign">
<param name="property">address</param>
</generator>
</id>
<property name="name"/>
<one-to-one name="address" class="mypack.Address"/>
</class>
</hibernate-mapping>
3)Primary key and foriegn key relationship in both the tables :-
Primary key in one table and foriegn key in another table. below is the hbm for this
<hibernate-mapping>
<class name="mypack.Person">
<id name="personId" type="int">
<generator class="increment"/>
</id>
<property name="name"/>
<many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/>
</class>
<class name="mypack.Address">
<id name="id" column="addressId" type="int">
<generator class="increment"/>
</id>
<property name="city"/>
<property name="state"/>
</class>
</hibernate-mapping>
folks please advise.
I really think that join table is overkill here for a one-to-one mapping. For the other 2 solutions, please take a look at the following Hibernate documentation, everything is explained : http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-onetoone.
For your second mapping (primary key association), both entities need to define a one-to-one mapping and one side of the relation also needs to specify the attribute constrained="true".
This is how I would write your second mapping :
<class name="mypack.Person" table="PERSONS">
<id name="id" type="int" column="PERSON_ID">
<generator class="increment"/>
</id>
<property name="name" type="string" column="NAME"/>
<one-to-one name="address" class="mypack.Address" cascade="all"/>
</class>
<class name="mypack.Address" table="ADDRESSES">
<id name="id" type="int" column="ADDRESS_ID">
<generator class="foreign">
<param name="property">address</param>
</generator>
</id>
<property name="city" type="string" column="CITY"/>
<property name="state" type="string" column="STATE"/>
<one-to-one name="person" class="mypack.Person" constrained="true"/>
</class>

Map a one-to-zero association with hibernate

Could somebody explain me how to map a one-to-zero association with hibernate (using mapping xml files NOT annotations)?
These are the rules:
A Menu can have 0 or 1 Area associated.
An Area belongs to a Menu. (Or, if it is easier for you to modelize it: An Area can have 0 or 1 Menu associated).
Here are the desired TABLES (I want to use foreign keys):
TABLE MENU:
ID
NAME
FKID_AREA
TABLE AREA:
ID
NAME
FKID_MENU
Thanks!
You should remove the FKID_AREA from the MENU table, because it's not needed (and is even a problem, since it's redendant with AREA.FKID_MENU).
For the rest, this is described in the Hibernate documentation on bidirectional one-to-one association mappings:
<class name="Area">
<id name="id" column="ID">
<generator class="native"/>
</id>
<many-to-one name="menu"
column="FKID_MENU"
unique="true"
not-null="false"/>
</class>
<class name="Menu">
<id name="id" column="ID">
<generator class="native"/>
</id>
<one-to-one name="area"
property-ref="menu"/>
</class>
You should remove FKID_MENU from AREA table. As per your description, your hbm mapping would be below
<class name="Menu">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" cloumn="name"/>
<many-to-one name="area" column="FKID_AREA" class="Area"/>
</class>
<class name="Area">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" cloumn="name"/>
</class>

Categories

Resources