How to select sub-level entity with Hibernate Criteria Query? - java

Let's say I have this named HQL (findRoomQuery):
select r from House h inner join h.roomList r
where h.address = :address and r.roomNo = :roomNo
The mapping of House entity is like this:
<class name="com.example.House" table="house">
<id name="id" column="id" type="long">
<generator class="assigned" />
</id>
<property name="address" column="address" type="string" length="100" not-null="false"/>
<set name="roomList" cascade="none" lazy="false" fetch="join" inverse="true">
<key>
<column name="house_id"/>
</key>
<one-to-many class="com.example.Room"/>
</set>
</class>
While the Room entity is like this:
<class name="com.example.Room" table="room">
<id name="id" column="id" type="long">
<generator class="assigned" />
</id>
<property name="houseId" column="house_id" type="long" not-null="true"/>
<property name="roomNo" column="room_no" type="string" length="4" not-null="false"/>
</class>
The relationship is a House can has one or many Room.
The code to execute the query is like this:
Query query = getSession().createQuery("findRoomQuery")
.setParameter("address", address)
.setParameter("roomNo", roomNo);
return query.list();
You can see the HQL return Room entities from select r (r is alias of h.roomList).
How to do the same thing with Hibernate Criteria Query?
Is it possible?

Try this one
Criteria criteria = session.createCriteria(House.class, "house");
criteria.createAlias("house.roomList", "roomList");
criteria.add(Restrictions.eq("house.address",address));
criteria.add(Restrictions.eq("roomList.roomNo", roomNo));
criteria.setProjection(Projections.property("roomList"));
Room r = (Room) criteria.uniqueResult();
replace
<property name="houseId" column="house_id" type="long" not-null="true"/>
to
<many-to-one name="houseId" class="com.example.House" fetch="select">
<column name="house_id" not-null="true" />
</many-to-one>

Related

HQL join returns only first two rows

I'm trying to join two tables and fetch all rows from an sql database using a hibernate query. I have two mapping files Offer.hmb.xml and Product.hbm.xml. In my Products class I have this function:
public List<Product> getProducts() {
factory = (new Configuration()).configure().buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
List<Product> products = new ArrayList<Product>();
Product q = new Product();
Query query = session.createQuery("select p from Product p JOIN p.offers o where p.offerID = o.offerID or p.offerID = null");
List<Product> list = query.list();
Iterator<Product> iter = list.iterator();
while (iter.hasNext()) {
Product product = iter.next();
System.out.println(product);
}
Product mapping:
<hibernate-mapping>
<class name="shoppingbasket.Product" table="products">
<id name="productID" type="integer" access="field">
<column name="ProductID" />
<generator class="assigned" />
</id>
<property name="offerID" type="java.lang.Integer" access="field">
<column name="OfferID" />
</property>
<property name="productName" type="java.lang.String" access="field">
<column name="ProductName" length="40" not-null="true"/>
</property>
<property name="unitPrice" type="java.math.BigDecimal" access="field">
<column name="UnitPrice"/>
</property>
<one-to-one name="offers" class="shoppingbasket.Offer" />
</class>
</hibernate-mapping>
Offer mapping:
<hibernate-mapping>
<class name="shoppingbasket.Offer" table="offers">
<id name="offerID" type="integer" access="field">
<column name="OfferID" />
<generator class="assigned" />
</id>
<property name="offerDescription" type="java.lang.String" access="field">
<column name="OfferDescription" length="60" not-null="true"/>
</property>
<property name="shortDescription" type="java.lang.String" access="field">
<column name="ShortDescription" length="10" not-null="false"/>
</property>
<property name="TFTPOTGroup" type="java.lang.Integer" access="field">
<column name="TFTPOTGroup" length="4" not-null="false" default="null"/>
</property>
<property name="discountPercentage" type="java.lang.Double" access="field">
<column name="DiscountPercentage" not-null="false" default="null"/>
</property>
</class>
</hibernate-mapping>
The query runs but only returns the first two rows however if I convert the query to SQL and run it in phpmyadmin it returns all rows. What am I doing wrong? I'm new to Java and HQL any pointers/help would be appreciated

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 make same aliases for different joins

Suppose, we have 3 entity
<class name="AEntity" table="A" abstract="true">
<id name="id">
<column name="ID"/>
</id>
<joined-subclass table="Aa" name="AaEntity">
<key column="key"/>
<many-to-one name="b" class="BEntity">
<column name="B_ID"/>
</many-to-one>
</joined-subclass>
<joined-subclass table="Ab" name="AbEntity">
<key column="key"/>
<many-to-one name="b" class="BEntity">
<column name="B_ID"/>
</many-to-one>
</joined-subclass>
</class>
<hibernate-mapping>
<class name="Parent" table="parent">
<id name="id">
<column name="ID"/>
</id>
<many-to-one name="a" class="AEntity">
<column name="a_ID" />
</many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="BEntity" table="B">
<id name="id">
<column name="ID"/>
</id>
</class>
</hibernate-mapping>
Then, we make criteria request
Criteria criteria = session.createCriteria(Parent.class);
criteria.createAlias("a","a");
criteria.createAlias("a.b","b");
criteria.list();
And get next sql
select this_.ID as ID1_5_3_,
this_.a_ID as a2_5_3_,
a1_.ID as ID1_2_0_,
a1_.B_ID as B2_2_0_,
a1_1_.B_ID as B2_0_0_,
a1_2_.B_ID as B2_1_0_,
case when a1_1_.key is not null then 1 when a1_2_.key is not null then 2 when a1_.ID is not null then 0 end as clazz_0_,
b2_.ID as ID1_3_1_,
b2_.C_ID as C2_3_1_,
b2_.ID as ID1_3_2_,
b2_.C_ID as C2_3_2_
from parent this_
inner join A a1_ on this_.a_ID=a1_.ID
left outer join Aa a1_1_ on a1_.ID=a1_1_.key
left outer join Ab a1_2_ on a1_.ID=a1_2_.key
inner join B b2_ on a1_1_.B_ID=b2_.ID
inner join B b2_ on a1_2_.B_ID=b2_.ID
We can see that B table joins two times and have same name b2_. Is it bug? And how make two joins but with different name of B table.
Okay?
Try aliases like
Criteria criteria = session.createCriteria(Parent.class, "parent");
criteria.createAlias("parent.a","a");
criteria.createAlias("parent.a.AaEntity.b","aab");
criteria.createAlias("parent.a.AbEntity.b","abb");
criteria.list();
PS: I have not tested this and it may not work as it is, but this will give you an idea on how to force the alias names.

org.hibernate.exception.MappingException or ConstraintViolation

I am using hibernate 4.1.9. I have Users, and the Users have a list of Accounts, and Accounts have list of Transactions. Here is my hbm.xml
<?xml version="1.0"?>
<class name="User" table="users">
<id name="userId" column="userid">
<generator class="increment"/>
</id>
<property name="username" column="username" not-null="true"/>
<property name="password" column="password" not-null="true"/>
<property name="registerDate" type="timestamp" column="register_date"/>
<list name="accounts" table="accounts" inverse="true" cascade="all">
<key column="userid" not-null="true"/>
<index column="accountid"/>
<one-to-many class="com.joe.data.Account"/>
</list>
</class>
<class name="Account" table="accounts">
<id name="accountId" column="accountid">
<generator class="increment"/>
</id>
<property name="balance" type="big_decimal" column="balance"/>
<property name="lastModified" type="timestamp" column="last_modified"/>
<list name="txns" table="transactions" inverse="true" cascade="all">
<key column="accountId" not-null="true"/>
<index column="transactionId"/>
<one-to-many class="com.joe.data.Transaction"/>
</list>
<many-to-one name="userId" class="User" column="userid" not-null="true"
unique="true" cascade="all"/>
<many-to-one name="accountType" class="AccountType" column="account_type"
not-null="true" cascade="all" unique="true" />
</class>
<class name="Transaction" table="transactions">
<id name="transactionId" column="transactionid">
<generator class="increment"/>
</id>
<property name="description" column="description"/>
<property name="amount" type="big_decimal" column="amount"/>
<property name="dateAdded" column="date_added"/>
<property name="reoccuring" type="numeric_boolean" column="reoccuring"/>
<many-to-one name="category" class="Category" column="category"
not-null="true" cascade="all" unique="true" />
</class>
<class name="Category" table="categories">
<id name="categoryId" column="categoryid"/>
<property name="categoryName" column="categoryname" not-null="true"/>
</class>
<class name="AccountType" table="account_types">
<id name="accountType" column="account_type"/>
<property name="accountName" column="name"/>
</class>
If I leave inverse="true" on the list of accounts (in the User) I get the ConstraintViolationException because the userid is not getting put in the insert query. If I take inverse="true" off of the list of accounts, I get org.hibernate.MappingException: Repeated column in mapping for entity: com.joe.data.Account column: accountid (should be mapped with insert="false" update="false")
To clarify lowercase names are database columns names, camel case are class variable names. I know Transaction class isn't working quite right yet, but if I could get the Accounts to insert I could do the same thing to get the Transactions to insert.
Edit: I added the many-to-one on the Account class and now I am getting another exception where hibernate is complaining about missing a getter for userId in com.joe.data.Account
In order to get inverse="true" work, you need to define many-to-one for User in Account and for Account in Transaction class and mappings.

Establish relationship from one table to another table in Hibernate

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.

Categories

Resources