Hibernate query/criteria returning duplicate data - java

criteria = createCriteria("employee");
criteria.add(Restrictions.eq("name", "Jack"));
criteria.createAlias("certificate", "cert");
criteria.add(Restrictions.eq("cert.certType", "MSFT"));
criteriaList = criteria.list();
Given the data below, I think the query above should have returned one record that contains a set(set size=2) of certificates but I get the same record duplicated twice(once for each record in Certificate table). Why is this happening?
Employee Table:
EMP_ID NAME
123 Jack
111 Mary
000 Larry
Certificate table data
emp_id certificate_type seq_no
123 MSFT 1
123 MSFT 2
111 English 1
employee.hbm.xml
<class name="com.Employee" table="Employee" entity-name="employee" mutable="false">
<cache usage="read-only"/>
<id name="id" column="employee_id"/>
<set name="certificate" fetch="select" inverse="true" lazy="false" >
<key column="employee_id" />
<one-to-many class="com.Certificate" entity-name="CertificateType"/>
</set>
</class>
certificate.hbm.xml
<class name="com.Certificate" table="Certificate" entity-name="CertificateType" mutable="false">
<cache usage="read-only"/>
<composite-id class="com.usps.nom.tops.model.impl.DispatchLegPKImpl" mapped="true">
<key-property name="empId" column="emp_id" />
<key-property name="seqNo" column="SEQ_NO" />
</composite-id>
<property name="certType" column="certificate_type"/>
</class>
POJOs
public class Employee {
private int id;
private String ame;
//getters and setters
public boolean equals(Object obj){}
}
public class Certificate {
private int emp_id;
private String certType;
private String seqNo;
//getters and setters
public boolean equals(Object obj){}
}
EDIT:
If I put the result (ie criteriaList in my example) in a set, then it gets rid of the duplicate record.
Set<Employee> empSet = new HashSet<Employee>(criteriaList);

I'm newbie in Hibernate, but faced with similar problem (parent records are duplicated by join)
I have added FetchMode.SUBSELECT annotation (I prefer annotations)
#OneToMany
#Fetch(FetchMode.SUBSELECT)
It looks like working perfectly for me without duplicating data.

Related

How to structure SQL Statement to avoid so many joins

I am trying to build a select statement which will allow me to build a local in app cache when my application starts up. My table structure looks as follows;
I know to get some help I must demonstrate what I have already attempted but its extremely
I am using hibernate which should make things easier but I am really hitting a road block, the only approach I can think of is to select all the fields and then do a left outer join on the table ids matching, however the issue with this approach is that one user can have 0-1 business cards/ 0-1 social cards, 0-1 personal cards which means when i left outer join some of the data potentially might be missing for a user. The personalInfo/Logindetails and Device Data is all 1:1 mapping
Is there a more efficient way to do the select in hibernate which will allow me to easily construct my cache?
Ideally I would like to construct an object such that;
public class User {
private BusinessCard businessCard;
private SocialCard socialCard;
private PersonalCard personalCard;
private PersonalInformation personalInformation;
private LoginDetails loginDetails;
private DeviceData deviceData;
public BusinessCard getBusinessCard() {
return businessCard;
}
public void setBusinessCard(BusinessCard businessCard) {
this.businessCard = businessCard;
}
public SocialCard getSocialCard() {
return socialCard;
}
public void setSocialCard(SocialCard socialCard) {
this.socialCard = socialCard;
}
public PersonalCard getPersonalCard() {
return personalCard;
}
public void setPersonalCard(PersonalCard personalCard) {
this.personalCard = personalCard;
}
public PersonalInformation getPersonalInformation() {
return personalInformation;
}
public void setPersonalInformation(PersonalInformation personalInformation) {
this.personalInformation = personalInformation;
}
public LoginDetails getLoginDetails() {
return loginDetails;
}
public void setLoginDetails(LoginDetails loginDetails) {
this.loginDetails = loginDetails;
}
public DeviceData getDeviceData() {
return deviceData;
}
public void setDeviceData(DeviceData deviceData) {
this.deviceData = deviceData;
}
}
Thanks
From Hibernate Community Documentation - Charpter 7 - Association Mappings:
7.2.1. Many-to-one
A unidirectional many-to-one association is the most common kind of unidirectional association.
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person (
personId bigint not null primary key
, addressId bigint not null
)
create table Address (
addressId bigint not null primary key
)
7.2.2. One-to-one
A unidirectional one-to-one association on a foreign key is almost identical. The only difference is the column unique constraint.
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person (
personId bigint not null primary key
, addressId bigint not null unique
)
create table Address (
addressId bigint not null primary key
)
This is how you may map one-to-one relationship. Notice this is the same sa many-to-one mapping, except for the unique constraint.
Now having mapped your classes, your Person class shall look like the following.
public class Person {
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
public int getId() { return id; }
protected void setId(int id) { this.id = id; }
private Address address;
private int id;
}
To answer your question
It all depends how you want the data to be persisted. As expressed, I would go with the identities of BusinessCard, SocialCard and PersonalCard into the User table so that one and only one card may exist per user.
<class name "User" table="Users">
<id name="id" column="userId">
<generator class="native" />
</id>
<many-to-one name="businessCard"
column="businessCardId"
unique="true"
not-null="true" />
<many-to-one name="socialCard"
column="socialCardId"
unique="true"
not-null="true" />
<many-to-one name="personalCard"
column="personalCardId"
unqiue="true"
not-null="true" />
</class>
<class name="BusinessCard" table="BusinessCards">
<id name="id" column="businessCardId">
<generator class="native" />
</id>
</class>
<class name="SocialCard" table="SocialCards">
<id name="id" column="socialCardId">
<generator class="native" />
</id>
</class>
<class name="PersonalCard" table="PersonalCards">
<id name="id" column="personalCardId">
<generator class="native" />
</id>
</class>

Why am I getting Mapping Exception?

I am getting :
org.hibernate.MappingException: Foreign key (FKBB979BF4266AA123:address [a_id]))
must have same number of columns as the referenced
primary key (address [p_id,a_id])
as I try to run the following (though incomplete) snippet :
public static void main(String args[]) {
Configuration config = new Configuration().configure();
SessionFactory sessFact = config.buildSessionFactory();
Session sess = sessFact.openSession();
Transaction trans = sess.beginTransaction();
}
The hibernate mapping xml is shown below :
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="address" cascade="all">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
<class name="pojo.Address" table="address">
<id column="a_id" name="addressID">
<generator class="foreign" />
</id>
<property name="address" column="address" />
</class>
I am trying a many to many association between Person and Address class.
What is the reason for this exception ?
I have created two tables person and address using these sql commands :
CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id));
CREATE TABLE address(a_id INTEGER,address TEXT);
POJO
Person
public class Person {
private int personID;
private String personName;
private Set addressSet;
public int getPersonID() {
return personID;
}
public void setPersonID(int personID) {
this.personID = personID;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Set getAddressSet() {
return addressSet;
}
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
Address
public class Address {
private int addressID;
private String address;
private Set personSet;
public int getAddressID() {
return addressID;
}
public void setAddressID(int addressID) {
this.addressID = addressID;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Set getPersonSet() {
return personSet;
}
public void setPersonSet(Set personSet) {
this.personSet = personSet;
}
}
For a ManyToMany Relationshhip you need a dedicated mapping table
6.2.4. Collections of values and many-to-many associations
i.e. You need something like a PersonAddress Table
CREATE TABLE personaddress (p_id integer, a_id integer)
Where p_id is a FK Reference to the Person Table and a_id a FK Reference to the Address Table
You need to specify different table name for many-to-many association as it's handled by a separate table:
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="person_address" cascade="all">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
Note that <set> now references to person_addresses table. With default configuration, Hibernate is able to create it automatically.
There's another mistake that I see: ID generator for Address entity should not be foreign, it's usually used in 1-to-1 relationships (uses ID of another associated object). You can use the same 'increment' use used for Person entity:
<class name="Address" table="address">
<id column="a_id" name="addressID">
<generator class="increment" />
</id>
<property name="address" column="address" />
</class>
You need to create a reference table:
CREATE TABLE PersonsAddresses (personId BIGINT, addressId BIGINT)
and change the mapping of the set to this:
<set name="addressSet" table="PersonsAddresses" order-by="personId">
<key column="personId" foreign-key="p_id"/>
<many-to-many column="addressId" node="a_id" class="pojo.Address" />
</set>

can we map non-object propety to an object in Many-to-one relationship of hibernate?

I have a very specific scenario as follow.
public class Person
{
Long id;
Collection<PersonRelation> personRelationCollection = new LinkedHashSet<PersonRelation>();
/**
has respective getter and setter
**/
}
public class PersonRelation
{
Long id;
Long parentPersonId; // here I don't want parentPersonId of type Person
Long childPersonId; // here also I don't want childPersonId of type Person
String relationType;
/**
has respective getter setter
**/
}
In my mapping files I have following
<class name="Person" table="PERSON">
<id name="id" column="IDENTIFIER">
<generator class="native"/>
</id>
<set
name="personRelationCollection"
table="PERSON_RELATION"
cascade="all"
>
<key column="PARENT_PERSON_ID"/>
<one-to-many class="PersonRelation"/>
</set>
</class>
and
<class name="PersonRelation" table="PERSON_RELATION">
<id name="id" column="IDENTIFIER">
<generator class="native"/>
</id>
<!-- following many-to-one mapping doesn't work-->
<!-- I need help here to satisfy my requirement -->
<many-to-one
name="parentPersonId"
column="PARENT_PERSON_ID"
class="Person"
not-null="true"/>
<Property name="childPersonId" column="CHILD_PERSON_ID"/>
<property name="relationType" column="RELATION_TYPE"/>
</class>
In this example, as in PersonRelation class, attribute parentPersonId is Long and not type of Person, I'm getting
org.hibernate.MappingException: Association references unmapped class PersonRelation
$
Please help.
Forget about references by id. In Hibernate you work with objects, not tables.
I guess your code could be written like this:
#Entity
#Table(name="your_table")
public class Item{
private Long id;
private Item parentItem;
private List<Item> children;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId(){
}
#ManyToOne()//Your options
public Item getParentItem(){
}
#OneToMane(mappedBy="parentItem")
public List<Item> getChildren(){
}
//Setters omitted
}
finally i found answer my own. Very small thing we have to do as follow.
<class name="PersonRelation" table="PERSON_RELATION">
<id name="id" column="IDENTIFIER">
<generator class="native"/>
</id>
<!-- here remove many-to-one mapping ---- it's not needed-->
<!-- treet participantPersonId as a simple property and everything will work -->
<Property name="parentPersonId" column="PARENT_PERSON_ID" type="Long"/>
<Property name="childPersonId" column="CHILD_PERSON_ID"/>
<property name="relationType" column="RELATION_TYPE"/>
</class>
This works perfectly fine. :)
Here, when you insert Person object, then it will not inset PersonRelation object too. You have to explicitly insert PersonRelation object. Perhaps, when we retrieve Person object, then it will gives you collection of PersonRelation. Here no need to retrieve PersonRelation collection explicitly.

Hibernate parent-child mapping

I'm trying to map an order and its order items in Hibernate. An order item should not be able to reference its parent order:
public class Order {
private long id;
private Set<OrderIter> orderItems = new HashSet<OrderItem>();
public long id() {
return id;
}
public void add(OrderItem item) {
item.setItemNumber(orderItems.size() + 1);
orderItems.add(item);
}
public Set<OrderItem> orderItems() {
return Sets.newHashSet(orderItems);
}
}
public class OrderItem {
private int itemNumber;
public int itemNumber() {
return itemNumber;
}
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
}
The objective is to have Hibernate automatically persist an order item when it's added to an order, like this:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Order order = (Order) session.load(Order.class, orderId);
OrderItem item = new OrderItem();
order.add(item);
// Done
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
I looked at Chapter 24. Example: Parent/Child, but in this example the child has a reference to the parent. I'm now trying to map it using Collections of dependent objects:
<class name="Order" table="ORDERS">
<id name="id" column="ORDER_ID" access="field">
<generator class="native" />
</id>
<set name="orders" table="ORDER_ITEMS" access="field">
<key column="id" />
<composite-element class="OrderItem">
<property name="ItemNumber" access="field" />
</composite-element>
</set>
</class>
This is almost working, but the combination of order id and item number should be unique. How can I meet all these criteria with a Hibernate mapping?
Here the one-to-many association between Order->OrderItem is mapped using a JOIN TABLE.
The one-to-many association is mapped with a many-to-many with unique set to true.
(since one-to-many is not aware of a join table on the set)
<class name="Order" table="ORDERS">
<set name="orders" table="ORDER_ORDERITEMS_RT">
<key column="ORDER_ID" />
<many-to-many name="OrderItem" unique="true" column="ORDERITEM_ID"/>
</set>
</class>
<class name="OrderItem table="ORDERITEMS">
</class>
The above mapping satisfies
OrderItem not having a reference to Order. As the mapping are in a separate table
One-to-many association makes the orderid-orderitemid pair unique.
You can put appropriate cascading on the set to allow saving the orderItem when added to the list on the Order. (Not shown in the mapping)
Hope this helps.

Hibernate: How to select a single item based on a foreign key column that is part of an association mapping?

Hullo,
So... I have the following objects:
public class Person {
// some other getters/setters omitted.
void setAddress(Address addy) {
// omitted
}
Address getAddress() {
// omitted
}
}
public class Address {
Integer getId() {
// omitted
}
}
And, I have the following hibernate mappings:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
So, there is a one to one mapping from Person to Address, Person has the foreign key to Address.
What I'm trying to do is fetch a Person object from a given Address ID... but I can't seem to figure out the correct HQL syntax:
public Person getPersonFromAddress(Address address) {
Query query = this.session.createQuery("select p from Person as p where p.address_id = " + address.getId());
#SuppressWarnings("unchecked")
Person p = (Person)query.uniqueResult();
return p;
}
I know I don't have a mapping from the foreign key column to a property on Person. Everytime I try to add one I get an exception saying I'm using the same column twice? I don't see what would be wrong with that :).
Anyways, what would be the best way to fetch the Person given an Address? Any help would be appreciated.
You should be able to do this
Query query = this.session.createQuery("select p from Person as p where p.address=:address")
.setParameter("address",address);

Categories

Resources