hibernate one to one on foreign key - java

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

Related

Can Hibernate automatically select after insert?

I am inserting an object into a table using Hibernate saveOrUpdate(object). This object does have an automatically generated ID field and also a 'createdDate' field that is set by the database upon insert. After the insert, the ID field in the java object has the new generated value, but the 'createdDate' field is still null.
Is there a way (configuration or setting or parm) that can tell Hibernate to select the new row automatically after the insert is done, so that the Java object in memory is completely filled out? After the user 'saves' the object the webpage stays open on this object for the user to edit more fields (that are only available after the initial insert), so I want to show these generated fields ( ID, createdDate ) to the user.
** EDIT **
Due to an answer, I'm adding the HBM.XML file for the object:
<hibernate-mapping>
<class name="MyObject" table="MyObject">
<id name="objectId" type="java.lang.Integer">
<column name="Object_ID" />
<generator class="identity" />
</id>
. . . other attributes . . .
<property name="createdDate" type="timestamp" update="false" insert="false">
<column name="Created_Date" not-null="true" />
</property>
</class>
</hibernate-mapping>
Annotate the property with #Generated
#Generated(GenerationTime.INSERT)
private Date creationDate;
Note that this will have a cost. You could simply generate the creationDate in the code, and not in the database (using a listener or a JPA #PrePersist annotation).

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.

java hibernate skip insert 'id' on insert

I have designed a table (in postgres) where 'id' column is autoincremented via its SEQUENCE entity.
But when I began use hibernate I met the problem .... due creating the insert statement, it use the follow statement
INSERT INTO mytable (id, name) VALUE (0, 'blablabla')
...
but I want it make somethink like that:
INSERT INTO mytable (name) VALUE ('blablabla')
... Postgres have to generate id automatically (at least when I ran such scripts withing sql editor, it worked)
I belive it can be configured, but I don't know how...
Here is my part of my .hbm.xml:
<hibernate-mapping>
<class name="com.cse.database.bean.Category" table="category">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="100" not-null="true" unique="true" />
</property>
</class>
</hibernate-mapping>
When you specify a <generator>, you're telling Hibernate how to create the ID. assigned means your application is doing it (which you don't want). You can specify a Hibernate class or even an application class to do it, which you don't want either. identity means the database does it for you, which is what you want. In some databases you can use sequence (which will query the sequence generator to get the ID, then write the record), and Hibernate allows you to use native to specify the most applicable for your DB.
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
apparently works in this case.
assigned means that you explicitely assign the ID of the entity, which is not the case. The problem with these auto-generated IDs in postgreSQL, AFAIK, is that it's not possible to get the last generated ID, and that Hibernate has no way to get the ID assigned to the entity after it has inserted it. You should not use an autogenerated ID in PostgreSQL.
Instead, let Hibernate use a sequence generator and assign the ID to the entity before inserting it:
#SequenceGenerator(name = "FOO_SEQ", sequenceName = "FOO_SEQ")
public class Foo {
#Id
#GeneratedValue(generator = "FOO_SEQ")
#Column(name = "FOO_ID")
private Long id;

Mapping two tables 0..n in Hibernate

I have a table Users
CREATE TABLE "USERS" (
"ID" NUMBER NOT NULL ,
"LOGINNAME" VARCHAR2 (150) NOT NULL )
and I have a second table SpecialUsers. No UserId can occur twice in the SpecialUsers table, and only a small subset of the ids of users in the Users table are contained in the SpecialUsers table.
CREATE TABLE "SPECIALUSERS" (
"USERID" NUMBER NOT NULL,
CONSTRAINT "PK_SPECIALUSERS" PRIMARY KEY ("USERID") )
ALTER TABLE "SPECIALUSERS" ADD CONSTRAINT "FK_SPECIALUSERS_USERID" FOREIGN KEY ("USERID")
REFERENCES "USERS" ("ID")
/
Mapping the Users table in Hibernate works ok
<hibernate-mapping package="com.initech.domain">
<class name="com.initech.User" table="USERS">
<id name="id" column="ID" type="java.lang.Long">
<meta attribute="use-in-tostring">true</meta>
<generator class="sequence">
<param name="sequence">SEQ_USERS_ID</param>
</generator>
</id>
<property name="loginName" column="LOGINNAME" type="java.lang.String" not-null="true">
<meta attribute="use-in-tostring">true</meta>
</property>
</class>
</hibernate-mapping>
But I'm struggling in creating the mapping for the SpecialUsers table. All the examples (e.g. in Hibernate documentation) in Internet I found don't have this type of self-reference. I tried a mapping like this:
<hibernate-mapping package="com.initech.domain">
<class name="com.initech.User" table="SPECIALUSERS">
<id name="id" column="USERID">
<meta attribute="use-in-tostring">true</meta>
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" class="User"/>
</class>
</hibernate-mapping>
but got the error
Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException:
Duplicate class/entity mapping com.initech.User
How should I map the SpecialUsers table? What I need on the application level is a list of the User objects contained in the SpecialUsers table.
How should I map the SpecialUsers table?
According to the structure of your USERS and SPECIALUSERS tables, you have a Table per subclass hierarchy and it is perfectly possible to map this kind of hierarchy with Hibernate.
First, make sure that SpecialUser inherits from User at the object model level (this is an inheritance relationship, not a self-reference).
public class SpecialUser extends User {
}
Then, declare a <joined-subclass> element in the User's mapping:
<hibernate-mapping package="com.initech.domain">
<class name="com.initech.User" table="USERS">
<id name="id" column="ID" type="java.lang.Long">
<meta attribute="use-in-tostring">true</meta>
<generator class="sequence">
<param name="sequence">SEQ_USERS_ID</param>
</generator>
</id>
<property name="loginName" column="LOGINNAME" type="java.lang.String" not-null="true">
<meta attribute="use-in-tostring">true</meta>
</property>
<joined-subclass name="com.initech.SpecialUser" table="SPECIALUSERS">
<key column="USERID"/>
</joined-subclass>
</class>
</hibernate-mapping>
What I need on the application level is a list of the User objects contained in the SpecialUsers table.
The following HQL query would return all the SpecialUser (which are User too):
from SpecialUser
If later you need to add columns to the SPECIALUSERS table, add the corresponding attributes to the SpecialUser class and map them inside the <joined-subclass> element. Refer to the provided link (to the documentation) for the details.
If you really have to use a second table, you must map it to a new class. Hibernate can always only map one table to one class.
Alternatively, add a column to the user table which says "this user is special". If that is not an option, create a view which joins the two tables and thus simulates this column. Then, you can select users by this column.
If you go the two class way, you must load a list of the special users and do the mapping in your application.
[EDIT] Maybe you should look into parent-child relations. In your case, the user is the parent and its roles are the children. So every user has 0..n roles which are mapped in a different table. You can then use this HQL
from User u where :role in elements(u.roles)
to find all users with a certain role. But most of the time, you'll have a specific user and just need to query whether she has a certain role.

Categories

Resources