Hibernate: mapping nested objects -EDITED- - java

For example, I have the following tables:
USER:
| USERID | USERNAME | USERTYPEID |
USERTYPE:
| USERTYPEID | USERTYPENAME |
So clearly USERTYPEID is a foreign key that a user use to refer to usertype. The JAVA implementation is as such:
I have a class User and a class UserType, where looks like:
public class User {
private int id;
private UserType ut;
....
}
public class UserType {
...
}
In the User.hbm.xml:
<hibernate-mapping>
<class name="com.pretech.User" table="User">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="UserID">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
So my questions follows:
1) what should I put to map the UserType here and indicate UserType as foreign key?
2) In the case of User includes a list of UserType (may not conceptually true but just want to use as an example), such as:
public class User {
private int id;
private List<UserType> uts;
}
what shoud I do about the hibernate mapping?
EDIT:
Added explanation about foreign key stuff.

There are so many examples available in net for your example, you can also check the hibernate documentation:
For example if you want to have a User entity with a set of UserType's then you can use one-to-many relationship and the mapping file will be:
<class name="User">
<id name="id" column="id">
<generator class="native"/>
</id>
<set name="uts">
<key column="userId"
not-null="true"/>
<one-to-many class="UserType"/>
</set>
</class>
<class name="UserType">
<id name="id" column="id">
<generator class="native"/>
</id>
</class>
And here is another example from the documentation that uses annotations and List instead of Set:

It should be OneToOne or OneToMany depending on UserType is a single reference or multiple (collection list).
Search on Google and you will get numerous tutorials with both xml based and annotations based

Related

Mapping in Hibernate on another field than the PK

I'm stuck for a moment now and I need some help.
I wanna have a mapping in hibernate between 2 fields that are not PK in any of the both Tables
Table Category (
catId Numeric(10),
categoryBusinessRef Numeric(10)
)
Table Product (
productId Numeric(10),
categoryBusinessRef Numeric(10)
)
The Sql query would be :
SELECT *
from Category as a
Join Product as b on a.categoryBusinessRef = b.categoryBusinessRef
But in Hibernate it make me this mapping
SELECT *
from Category as a
Join Product as b on a.categoryId = b.categoryBusinessRef
My hbms looks likes
<class name="Category" table="A">
<id name="categoryId" length="10">
<column name="categoryid" />
<generator class="sequence">
<param name="sequence">S_category</param>
</generator>
</id>
<set name="categoryBusinessRefs" table="B" >
<key>
<column name="categoryBusinessRef" />
</key>
<one-to-many class="ProductClass" />
</set>
</class>
<class name="Product" table="B">
<id name="productId" length="10">
<column name="productid" />
<generator class="sequence">
<param name="sequence">S_product</param>
</generator>
</id>
<property name="categoryBusinessRef" length="10">
<column name="categoryBusinessRef" />
</property>
</class>
So it's a One-to-many relation but It have to map with another values as the PK of Category
Thanks for the help
EDIT :
If I can't do that ok ! But If I can I want to do that, I know all the stuff with the primary key but my question is not "Mapping in Hibernate on PK" but "Mapping in Hibernate on another field than the PK", so the answers who said map to the PK doesn't interest me :p
Your model is wrong, unless you want to have a cartesian product (it is a rare requirement) you should always join by the primary key.
You can have one-to-one, one-to-many or many-to-many relationships (the last two are more common).
In one-to-many you need the pk of the one side in the many side.
In many-to-may you need a relationship table that makes the mapping.
Assuming that you want you want that many B's connect to the same A (so one-to-many).
CREATE TABLE a (
id NUMERIC(10) PRIMARY KEY,
disc NUMERIC(10) NOT NULL
)
CREATE TABLE b (
id NUMERIC(10) PRIMARY KEY,
a_id NUMERIC(10) NOT NUL REFERENCES a (id)
)
Now you can query
SELECT *
FROM a
JOIN b ON b.a_id = a.id
and recover all discs from a with the proper relation to all b.

Hibernate one-to-many mapping configuration

I am new to hibernate and I am trying to implement one-to-many relationship with cascade loading and updating. However, my code always generates org.apache.jasper.JasperException: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister exception. Could you take a look on my attempt and suggest what am I doing wrong?
The general idea is that there is a Company class which contains set of customers. When creating new instance of customer, I add him to a company and persist everything.
Entities (only displaying relevant parts, or at least I hope so)
public class Company implements Serializable {
private static final long serialVersionUID = 146243652;
private String id;
private String name;
private String website;
private Set<Customer> customers;
... getters, setters etc
public class Customer implements Serializable {
private static final long serialVersionUID = 864235654;
private String name;
private String surname;
private String adress;
private String id;
private Company company;
customer.hbm.xml:
<hibernate-mapping>
<class name="wa2.entities.Customer" table="CUSTOMER">
<id column="ID" name="id" type="java.lang.String" />
<property column="NAME" name="name" type="java.lang.String" />
<property column="SURNAME" name="surname" type="java.lang.String" />
<property column="ADRESS" name="adress" type="java.lang.String" />
<many-to-one name="COMPANY" class="wa2.entities.Company">
<column name="COMPANY_ID" not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>
company.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="wa2.entities.Company" table="COMPANY">
<id column="ID" name="id" type="java.lang.String" />
<property column="NAME" name="name" type="java.lang.String" />
<property column="WEBSITE" name="website" type="java.lang.String" />
<set name="CUSTOMERS" table="CUSTOMER" fetch="select">
<key>
<column name="CUSTOMER_ID" not-null="true"></column>
</key>
<one-to-many class="wa2.entities.Customer"/>
</set>
</class>
</hibernate-mapping>
Saving customer (this worked before the implementation of one-to-many relationship so I doubt that there is anything wrong with this code):
Customer customer = new Customer();
String name = req.getParameter("name");
customer.setName(name);
String surname = req.getParameter("surname");
customer.setSurname(surname);
String adress = req.getParameter("adress");
customer.setAdress(adress);
String companyID = req.getParameter("companyId");
Company company = repository.loadCompany(companyID);
customer.setCompany(company);
String id = UUID.randomUUID().toString();
customer.setId(id);
repository.saveCustomer(customer);
I hope that I did not forget any relevant part. If so, please tell me and I will post the relevant code. Thank you very much for any help!
EDIT:
Thanks for the responses. It seems that I wrote the names wrongly in capitals. I changed that and now I am getting failed to lazily initialize a collection of role: wa2.entities.Company.customers, could not initialize proxy - no Session
since this is an mapping exception, i guess it comes up during session factory creation, and not when saving? also, seeing the whole stacktrace could help. your exception can be caused by a mismatch of property names between mapping file and beans. see Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister.
in your code you have the name attribute values in
<many-to-one name="COMPANY" class="wa2.entities.Company">
<column name="COMPANY_ID" not-null="true"></column>
</many-to-one>
and
<set name="CUSTOMERS" table="CUSTOMER" fetch="select">
<key>
<column name="CUSTOMER_ID" not-null="true"></column>
</key>
<one-to-many class="wa2.entities.Customer"/>
</set>
in uppercase, while the properties in your pojos are named company and customer.
One principle in hibernate, that is often stated, but even more often overseen, is, that you need to keep a clean object tree, or the database will not be kept in sync properly.
I think this causes your problem: You need to keep the tree clean. But you don't: You add the customer to the company on the customer side, but you do not add the customer to the company's customer set.
In your code, you have added a vice-versa link between customer and company. I would add an inverse to the link, for this is what you want.
Second: If you start with hibernate now, really think about the annotation approach. If you inherit legacy apps, stay with the XML, but if you start out fresh ...

hibernate: left outer join with Criteria API, is possible?

I'm new on Hibernate so excuse me for banality but I can't find any answer related to my problem (i tried to search on docs & on SO).
If possible, I would create a left outer join from two tables (t0 and t1) without using HQL; in my case I'd like to use only the Criteria API.
t0 { id, fieldA, fieldB }
t1 { id, fieldC, fieldD }
I don't know which fields will be used for the join, the user can decide it.
I see that the Criteria API has some nice functions such as createAlias or createCriteria but if I use these methods, I can't run a join.
Every table has a class (mapped with a specific hbm.xml) like this:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<class name="it.class.T0" table="t0">
<meta attribute="class-description">Table 0</meta>
<!-- Unique id -->
<id name="id" type="long" column="id">
<generator class="native"/>
</id>
<!-- Natural key -->
<natural-id mutable="true">
<property name="fieldA" type="string" column="fieldA" not-null="true" />
<property name="fieldB" type="string" column="fieldB" not-null="true" />
</natural-id>
<!-- Fields -->
<property name="column1" type="long" column="columnd1" not-null="true" />
<property name="column2" type="string" column="column2" not-null="false" />
</class>
</hibernate-mapping>
The entity class is like this:
public class T0 implements Serializable
{
private static final long serialVersionUID = -8123118569358735984L;
public long id;
public String fieldA;
public String fieldB;
public long column1;
public String column2;
public T0()
{
super();
}
}
Is it possible to create a left outer join programmatically (with the Criteria API) without specifying in the hbm.xml (or in a specific HQL) which fields to use?
No. You can't use a hibernate api to create a join based on a mapping hibernate doesn't know anything about. It kind of defeats the purpose of using hibernate in the first place. Why not just write sql then? ;-)

Creating unique Index for table in hibernate with one column being a foreign key

well I am not that advanced in hibernate and I tried to search a lot, found lots of similar questions but none really applying my case. I have a table TABLE1 which has an index "id" and another table which has as index (TABLE1_ID and NUMBER). Only TABLE1_ID is a foreign key while number doesn't reference anything specific
TABLE 1 has the following hibernate mapping
<class name="com.test.basic.BASICTABLE1"
entity-name="com.test.TABLE1" table="TABLE1"
dynamic-update="true" optimistic-lock="version">
<id name="id" type="long">
<column name="id" />
<generator class="native">
<param name="sequence">${table1_id.generator.sequence}</param>
</generator>
</id>
<version name="versionNumber" column="verno" generated="always"
access="field" />
<property name="column1" column="column1" type="string" length="19"
not-null="true" access="field" />
</class>
I am not sure what should be the mapping for the other table. I did it the following way
<class name="com.test.basic.BASICTABLE2"
entity-name="com.test.TABLE2" table="table2"
dynamic-update="true" optimistic-lock="version">
<composite-id name="id" class="com.test.basic.TABLE1TABLE2Id" >
<key-property name="TABLE1_ID" column="TABLE1_ID" type="long" />
<key-property name="NUMBER" column="NUMBER" type="short"/>
</composite-id>
</class>
Please note that I created the class TABLE1TABLE2Id after I read somewhere that there should be some intermediate mapping.
I'm sure there's something wrong with TABLE2 mapping (I am not so advanced with hibernate) but when trying to install the app, I am getting the following error
Foreign key (TABLE1 [id]) must have same number of columns as the referenced primary key (TABLE2 [TABLE1_ID, NUMBER])
I appreciate anybody's help thanks :)
Identity copy (foreign generator)
Finally, you can ask Hibernate to copy the identifier from another
associated entity. In the Hibernate jargon, it is known as a foreign
generator but the JPA mapping reads better and is encouraged.
The primary key from one entity might be as foreign key be primary key for another table.
#Entity
class MedicalHistory implements Serializable {
#Id Integer id;
#MapsId #OneToOne
#JoinColumn(name = "patient_id")
Person patient;
}
#Entity
class Person {
#Id #GeneratedValue Integer id;
}

JPA xml mapping not finding fields

I have the following class:
package lt.vic.valdos.domain.valda;
public class Valda implements java.io.Serializable {
private long id;
private Long valdosKodas;
public long getId() {
return id;
}
public Long getValdosKodas() {
return valdosKodas;
}
}
and the following orm.xml:
<?xml version="1.0"?>
<entity-mappings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_1.xsd"
version="2.1">
<entity class="lt.vic.valdos.domain.valda.Valda">
<table name="VALDOS" schema="VLD" />
<attributes>
<id name="id" />
<basic name="id">
<column name="vld_id" />
<return-insert return-only="true" />
</basic>
<basic name="valdosKodas">
<column name="valdos_kodas" />
</basic>
</attributes>
</entity>
</entity-mappings>
When I deploy this in glassfish, i get the following error:
Exception [EclipseLink-7215] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Could not load the field named [id] on the class [class lt.vic.valdos.domain.valda.Valda]. Ensure there is a corresponding field with that name defined on the class.
The class is in a jar that is included into a web application as a maven dependency. The orm.xml is in /WEB-INF/classes/META-INF of the web application.
What am I doing wrong?
Figured this one out myself. For some reason EclipseLink requires a setter on a class. Once I add private setters everything seems fine. Why the setters are needed (mapping accessors should default to FIELD) remains a mystery but it is not that important for me. Adding access="FIELD" to all entity attributes also fixes the problem without the setters.
You should be specifying the id as generated using the IDENTITY strategy:
<id name="id">
<column name="vld_id"/>
<generated-value strategy="IDENTITY"/>
</id>
This strategy will automatically read the database provided id back into the new object upon successful commit. The EclipseLink returning statement functionality is only applicable to basic mappings because id is already covered by Identity ID generation.
I think you have to add the column description for the id column to your id element, instead of using an extra basic element. As in <id name="id"> <column name="vld_id" /> ... </id>, without an extra <basic name="id"> ....
From my own experience (some time ago now), it's probably easier to use annotations to define your mappings.

Categories

Resources