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

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? ;-)

Related

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 ...

Beginner hibernate: Why does my hibernate query return only one result, instead of a list?

I'm very new to hibernate, and I'm trying to set up a new method in our PersonDAO.
My hibernate mapping file looks 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 package="com.foo.bar.domain">
<class name="Person" table="person">
<meta attribute="class-description">A Person</meta>
<id name="id" type="java.lang.Long" column="rid" unsaved-value="null">
<generator class="native" />
</id>
<version name="version" type="integer" column="rversion" unsaved-value="null" />
<property name="UID" type="string" column="UID" length="16" not-null="true" unique="true"/>
<property name="lastName" type="string" column="last_name" not-null="true" />
<property name="firstName" type="string" column="first_name" not-null="true" />
<property name="ownDepartment" type="string" column="own_department"/>
<!-- a person has many responsibilities and a responsibility can can assigned to many person -->
<set name="responsibilities" table="person_responsibility">
<key column="person_id"/>
<many-to-many column="responsibility_id" class="Responsibility"/>
</set>
<set name="additionalDepartments" table="PERSON_TO_ADDL_DEPARTMENT">
<key column="person_id"/>
<element column="ADDITIONAL_DEPARTMENT" type="string"/>
</set>
</class>
and I've written a method like this, in java, to fetch all the managers from a given department:
public List<Person> getManagerByDepartment(final String givenDepartment){
List<Person> l = (List<Person>) this.getHibernateTemplate().executeFind(new HibernateCallback<List<Person>>() {
public List<Person> doInHibernate(Session session) throws HibernateException, SQLException {
String query = "select p from Person p join p.responsibilities responsibilities join p.additionalDepartments additionalDepartments where responsibilities.name = 'manager' and (p.ownDepartment = :givenDepartment or additionalDepartments = :givenDepartment)";
List<Person> result = (List<Person>) session.createQuery(query).setString("givenDepartment", givenDepartment).list();
return result;
}
});
return l;
}
now I do a manual query in SQL, and I can see that for a given department, there are definitely more than one people who have the additional responsibility 'manager'...why does my method only ever return one person, instead of all of them?
I have a strong suspicion that my method, specifically my query, and not the mapping, is the issue, but I can't see what's wrong with it...
I've jumped in at the deep end here, so any help would be very much appreciated.
edit: note, I'm working on hundreds of records, not millions, and this isn't exactly a bottle-neck operation, so I'm not too worried about performance...that said if I'm doing something that's pointlessly wasteful, do point it out
You can print hibernate query by enabling showsql option and check the query getting created and then test it against the database.
Hard to be sure without the sample data, but when you do join in HQL, it is translated to inner join in SQL. So, if you know that there should be more than one result with given responsibility then the problem is probably join p.additionalDepartments.
Try this query with left join for additionalDepartments and see if it works
String query = "select p from Person p join p.responsibilities responsibilities left join p.additionalDepartments additionalDepartments where responsibilities.name = 'manager' and (p.ownDepartment = :givenDepartment or additionalDepartments = :givenDepartment)";

Hibernate: mapping nested objects -EDITED-

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

How to add collections to a data transfer class in Hibernate

I have the following setup where a class contains a collection. When querying instances of this class I like to populate a data transfer class rather than the data class. However, Hibernate generates a wrong SQL query. What am I missing?
The Hibernate mapping:
<class name="Thread" table="tbl_threads" schema="dbo">
<id name="Id" type="integer">
<column name="i_id"/>
<generator class="identity"/>
</id>
<set name="keywords" inverse="true" lazy="false" cascade="all-delete-orphan" optimistic-lock="false">
<key>
<column name="thread_id" not-null="true"/>
</key>
<one-to-many class="Comment"/>
</set>
<!-- ... -->
</class>
and
<class name="ThreadKeyword" table="tbl_keywords" schema="dbo">
<composite-id name="id"
class="se.ericsson.eab.sdk.fido.server.api.pojos.report.ReportThreadKeywordId">
<key-property name="keywordId" type="integer">
<column name="keyword_id" />
</key-property>
<key-property name="threadId" type="integer">
<column name="thread_id" />
</key-property>
</composite-id>
<!-- ... -->
</class>
The HQL I am using is
SELECT new Composite(t.id, t.keywords, ...)
FROM Thread t, ThreadKeyword tk
WHERE t.id = tk.id.threadId
This generates a SQL where the SELECT part contains only a dot for the keyword attribute:
select thread1_.report_id as col_0_0_, . as col_92_0_
from dbo.tbl_thread reportthre0_ inner join
dbo.tbl_keywords keywords4_ on reportthre0_.i_id=keywords4_.thread_id
It works fine when I query for the data class directly, i.e.
SELECT t
FROM Thread t, ThreadKeyword tk
WHERE t.id = tk.id.threadId
As I understand will Hibernate not find a column name for keywords in the thread table. That is right, since it is a collection. It rather needs to be populated using subsequent queries. If I omit the keywords in the constructor for the Composite class the query gets right but Hibernate won't populate the Set.
How do I get the keywords set populated?
You cannot do that with a collection.
t.id is a column/value
so Hibernate translates that into thread1_.report_id as col_0_0_. Hibernate even gave it the alias col_0_0_
t.keywords is a set of values so Hibernate just can't translate the collection into a column/value.
A query includes a list of columns to be included in the final result
immediately following the SELECT keyword - Wikipedia
Now the
SELECT t FROM Thread t, ThreadKeyword tk WHERE t.id = tk.id.threadId
works fine because Hibernate knows how to translate the query you have there into SQL.

Hibernate - how to map an EnumSet

I've a Color Enum
public enum color { GREEN, WHITE, RED }
and I have MyEntity that contains it.
public class MyEntity {
private Set<Color> colors;
...
I already have a UserType to map my Enums.
Do you know how to map a Set of Enums in the Hibernate hbm.xml?
Do I need a UserType or there's an easiest way? Thanks
edit: Just to remark, I'm looking for the hbm.xml configuration not the #CollectionOfElements Annotation
I use the solution from the EnumSet mapping thread which relies on the use of <element column>. You just need a table with an id and a string to map the collection (MYENTITY_COLOR here). And the mapping looks like that (the EnumUserType is the one from Java 5 EnumUserType):
<?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>
<typedef name="color" class="com.stackoverflow.q2402869.EnumUserType">
<param name="enumClassName">com.stackoverflow.q2402869.Color</param>
</typedef>
<class name="com.stackoverflow.q2402869.MyEntity" entity-name="MyEntity" table="MYENTITY">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<set name="colors" table="MYENTITY_COLORS">
<key column="ID" not-null="true"/>
<element type="color" column="COLOR"/>
</set>
</class>
</hibernate-mapping>
Query might look like this:
select distinct e from MyEntity e join e.colors colors where colors IN ('WHITE', 'GREEN')
The whole solution works well for loads, saves and queries (credits to jasonab).
It seems you need to use the #CollectionOfElements annotation. The doc is at http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection-extratype, chapter '2.4.6.2.5. Collection of element or composite elements'. The example also maps a Set of Enum.

Categories

Resources