One object from two tables - java

I have two tables and want to map them to one object with Hibernate.
The reason for both tables is in the past and I wont change the frontend, which access data like this.
I have Table Event (Event_ID, Preview, img) and Event_Details (ID, Event_ID, content).
I prepare one class in Java:
public class Event {
private int event_ID;
private String preview;
private String img;
private String content;
//Getter and Setter
}
and following XML mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16.03.2016 20:33:10 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="de.data.events.Event" table="ev1_event">
<id name="id" type="int">
<column name="Event_ID" />
<generator class="assigned" />
</id>
<property name="preview" type="java.lang.String">
<column name="ev1_preview" />
</property>
<property name="img" type="java.lang.String">
<column name="ev1_img" />
</property>
</class>
<class name="de.data.events.Event" table="pb1_event">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
//some properties
</class>
The part, where I have to join table1 to table2 is missing. But I didn´t found a way to fix my problem.

First, you'd have your Hibernate entities. Yours are Event and EventDetail, but for fun, let's go with Person and Address in this example. The key here is that you need a one-to-one or many-to-one relationship between your tables. Otherwise, your resultset will come out weird (more on that later).
#Entity
#Table(name = "PERSON")
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "PERSON_ID")
public Integer id;
#Column(name = "NAME")
public String name;
#Column(name = "HOME_ADDRESS_ID")
public Integer homeAddressId;
#Column(name = "TEMP_ADDRESS_ID")
public Integer tempAddressId;
// . . . other fields,getters,setters
}
#Entity
#Table(name = "ADDRESS")
public class Address {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ADDRESS_ID")
public Integer id;
#Column(name = "STREET")
public String street;
#Column(name = "CITY")
public String city;
// . . . other fields,getters,setters
}
Then you'd have your target FlatObject POJO with the a constructor that will build it appropriately:
public class FlatObject {
public final String name;
public final String homeStreet;
public final String homeCity;
public final String tempStreet;
public final String tempCity;
public FlatEntity(String name, String homeStreet, String homeCity, String tempStreet, String tempCity) {
this.name = name;
this.homeStreet = homeStreet;
this.homeCity = homeCity;
this.tempStreet = tempStreet;
this.tempCity = tempCity;
}
// . . . other fields,getters
}
Finally, you'd leverage those objects with a Hibernate HQL SELECT where you join Person and Address and use their fields to construct a new FlatEntity:
SELECT new FlatEntity(p.name, ha.homeStreet, ha.homeCity, ta.tempStreet, ta.tempCity)
FROM
Person p, Address ha, Address ta
WHERE
p.homeAddressId = ha.id
and p.tempAddressId = ta.id
As you can see, the HQL statment will join Person to Address twice: once for the home address and once for the temp address.
The same will hold true in your case. So in your case, if you're joining Event e, EventDetail ed WHERE e.id = ed.eventId, just be sure there's only one detail row per Event. Otherwise you'll get multiple rows when you have more than one details or you'll drop rows (because of the inner join) when an event has no details.

Related

How to connect Hibernate with Postgres in Intelij using Maven? [duplicate]

I have written few codes for inserting data into my SQL database. Actually, I am trying to learn Struts 2 with Hibernate. But, unfortunately I am facing a problem after submitting my form.
I could not find the reason for this error message. My try & catch block throws error like:
Exception in saveOrUpdate() Rollback :org.hibernate.MappingException: Unknown entity: v.esoft.pojos.Employee
Pojo(Employee.java):
#Entity
#Table(name = "employee", catalog = "eventusdb")
public class Employee implements java.io.Serializable {
private Integer empId;
private String name;
private String website;
public Employee() {
}
public Employee(String name, String website) {
this.name = name;
this.website = website;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "emp_id", unique = true, nullable = false)
public Integer getEmpId() {
return this.empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
#Column(name = "name", nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
#Column(name = "website", nullable = false, length = 65535)
public String getWebsite() {
return this.website;
}
public void setWebsite(String website) {
this.website = website;
}
}
also having Employee.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 10, 2013 4:29:04 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="v.esoft.pojos.Employee" table="employee" catalog="eventusdb">
<id name="empId" type="java.lang.Integer">
<column name="emp_id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" not-null="true" />
</property>
<property name="website" type="string">
<column name="website" length="65535" not-null="true" />
</property>
</class>
</hibernate-mapping>
If the entity isn't mapped, you should inspect the hibernate configuration. Hibernate is ORM framework used to map pojos (entities) to the database schema objects. You didn't configure or hibernate couldn't find mapping for the object Employee. The configuration file is hibernate.cfg.xml should contain the mapping to the resource Employee.hbm.xml. Suppose this file is in the same folder as Employee class. Then the mapping will be
<mapping resource="v/esoft/pojos/Employee.hbm.xml"/>
Another approach if you used an annotation based configuration, then you should use class attribute to map to the pojo that contains Hibernate/JPA annotations.
<mapping class="v.esoft.pojos.Employee"/>
Note, annotation based Configuration might be different depending on version of Hibernate and may require additional libraries.

Hibernate mapping: creating FK relation in existing db for join operations

I have an existing db that I cannot alter. There are two tables, one (A) with a PK and one (B) with a FK pointing to the PK of the first. The respective columns are named (A).page_id and (B).cl_from.
In Hibernate mapping i can set an id for the first, mapping the PK. I can also set a bag for the one-to-many relationship.
For the second, I don't need an id, but a many-to-one relationship. How can you set a many-to-one relationship without an id? I don't want to insert an id to the class..
I've tried to set a composite key but had no luck..
// The PK class
<class name="Words" table="PAGE">
<id name="id" column="PAGE_ID" />
<property name="text" column="PAGE_TITLE" />
<bag cascade="all-delete-orphan" inverse="true" lazy="false" name="wordPages">
<key column="PAGE_ID"/>
<one-to-many class="CategoryItems"/>
</bag>
</class>
// The FK class
<class name="CategoryItems" table="CATEGORYLINKS" >
<composite-id>
<key-many-to-one name="id" column="CL_FROM" />
</composite-id>
<property name="text" column="CL_TO" />
</class>
And my classes:
public class Words {
private Long id;
private String text;
private ArrayList wordPages;
// getters - setters
public class CategoryItems implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String text;
//getters-setters
The latest error i get is: An association from the table CATEGORYLINKS refers to an unmapped class: java.lang.Long
should help with the following code:
public class CategoryItems implements Serializable
{
private static final long serialVersionUID = 1L;
private Words id;
private String text;
//getters-setters
}

Provided id of the wrong type hibernate

I am getting error:
org.hibernate.TypeMismatchException: Provided id of the wrong type for class BEntity. Expected: class BEntity, got class AEntity
public class BEntity implements Serializable{
#Id
#Column(name = "NUM")
private String num;
#Id
#Column(name = "INIT")
private String init;
#Column(name = "V_CNT")
private Integer vcnt;
//{{{some column omitted}}}//
}
public class AEntity implements Serializable{
#Id
#Column(name = "NUM")
private String num;
#Id
#Column(name = "INIT")
private String init;
#OneToOne
#PrimaryKeyJoinColumns({
#PrimaryKeyJoinColumn(name="NUM", referencedColumnName="NUM"),
#PrimaryKeyJoinColumn(name="INIT", referencedColumnName="INIT")
})
private BEntity bEntity;
}
HQL query:
String queryString = "FROM AEntity AS A " +
"LEFT JOIN A.bEntityAS B " +
"WHERE A.INIT||A.NUM IN (:carList) AND A.INIT IN (:initList) AND A.NUM IN (:numberList) " +
"AND B.TRUK_AXL_CNT > 0";
Hibernate gen-code
select aentity0_.NUMBER as NUMBER4_0_, aentity0_.INITIAL as INITIAL4_0_, bentity_p1_.NUMBER as NUMBER5_1_, bentity_p1_.INITIAL as INITIAL5_1_, aentity0_.V_CNT as VCNT3_4_0_, aentity0_.EIN as EIN4_0_, aentity0_.TYP as TYP5_4_0_, aentity0_.TRUK_CNT as TRUK6_4_0_, bentity_p1_.TRUK_AXL_CNT as TRUK3_5_1_ from USR.aentity aentity0_ left outer join USR.bentity_PRIMARY bentity_p1_ on aentity0_.NUMBER=bentity_p1_.NUMBER and aentity0_.INITIAL=bentity_p1_.INITIAL
where (aentity0_.INITIAL||aentity0_.NUMBER in (?,?,?))
and (aentity0_.INITIAL in (?,?,?))
and (aentity0_.NUMBER in (?, ?, ?))
and bentity_p1_.TRUK_AXL_CNT>0
When I run the code in SQL Explorer it works only running it in code cause the issue...
Looks like this is a defect in hibernate version 3.2.6 which is still not resolved. Came across this JIRA.
Having multiple #Id is supported by Hibernate but seems it fails under one to one mapping, suggested way of resolving this is to use single CompositeKey, which means you create a PK class
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
#Embeddable
public class PKClass implements Serializable {
#Column(name = "NUM")
private String num;
#Column(name = "INIT")
private String init;
//gettter setter here
}
then in your Entity use this as the ID
public class BEntity implements Serializable{
#Id
private PKClass pkClass = null;
#Column(name = "V_CNT")
private Integer vcnt;
//{{{some column omitted}}}//
}
public class AEntity implements Serializable{
#Id
private PKClass pkClass = null;
#OneToOne
#PrimaryKeyJoinColumns({
#PrimaryKeyJoinColumn(name="NUM", referencedColumnName="NUM"),
#PrimaryKeyJoinColumn(name="INIT", referencedColumnName="INIT")
})
private BEntity bEntity;
}
I faced the same issue. I had two separate PK classes that had the same fields. So I removed one PK and used only one in the owner and child entity. This solved the problem.
I had the same problem.
Started situation
Primary key on master table
<composite-id name="id" class="model.E1Id">
<key-property name="taxpayerId" type="int">
<column name="TaxpayerID" />
</key-property>
<key-property name="year" type="int">
<column name="Year" />
</key-property>
</composite-id>
Primary key on detail table
<composite-id name="id" class="model.E1dataFromTaxPayerFolderId">
<key-property name="tid" type="int">
<column name="tID" />
</key-property>
<key-property name="year" type="int">
<column name="Year" />
</key-property>
</composite-id>
After repair
I change the class of detail table composite-id and i set the class of primary table like this:
<composite-id name="id" class="model.E1Id">
<key-property name="tid" type="int">
<column name="tID" />
</key-property>
<key-property name="year" type="int">
<column name="Year" />
</key-property>
</composite-id>
After that is necessary (if you have different names) to add in your primarykey class (in my case E1Id) the property tid (with getter and setter methods)

one to one relationship with hibernate

I try to create a one to one relationship between two tables.
One of them is Person:
public class Person implements Serializable {
static final long serialVersionUID = 1L;
private long id;
private String _email;
private String _pass;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmail() {
return _email;
}
public void set_email(String _email) {
this._email = _email;
}
public String getPass() {
return _pass;
}
public void set_pass(String _pass) {
this._pass = _pass;
}
}
and the second is ReqC2dmRegId table:
public class ReqC2dmRegId implements Serializable {
private static final long serialVersionUID = 1L;
Person person;
String C2dmid;
private long id;
public ReqC2dmRegId(){}
public String getC2dmid() {
return C2dmid;
}
public void setC2dmid(String c2dmid) {
C2dmid = c2dmid;
}
public ReqC2dmRegId(Person person, String C2dmid) {
super();
this.person = person;
this.C2dmid = C2dmid;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Now, in my program, I always create the Person first and only when I need I add this ReqC2dmRegId.
Now, what I try to do is to link this two tables. I mean, when I persist this ReqC2dmRegId (of course I add to the person in ReqC2dmRegId the right id) I want my ReqC2dmRegId to update or save a new row with the right Person id.
These are my hbm files:
ReqC2dmRegId.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 26, 2012 11:29:57 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="c2dm.ReqC2dmRegId" table="REQC2DMREGID">
<id name="id" type="long">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" class="Entities.Person" cascade="all" />
<property name="C2dmid" type="java.lang.String">
<column name="C2DMID" />
</property>
</class>
</hibernate-mapping>
Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 26, 2012 11:29:57 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="Entities.Person" table="PERSON">
<id name="id" type="long">
<column name="ID" />
<generator class="increment" />
</id>
<property name="_email" type="java.lang.String" access="field">
<column name="_EMAIL" />
</property>
<property name="_pass" type="java.lang.String" access="field">
<column name="_PASS" />
</property>
</class>
</hibernate-mapping>
What am I doing wrong?
When I try to run:
//this should to update or save the object in DB
public void update (Object query){
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
//em.createNativeQuery(query).executeUpdate();
em.merge(query);
em.flush();
em.getTransaction().commit();
em.close();
}
I get :
attempted to assign id from null one-to-one property:Person
In the end, it should look like this:
Person
**id email _pass**
2 lala#gmail.com 1234
ReqC2dmRegId
**id REQC2DMREGID**
2 ffgghhjj
Update:
after i gave up try to understand way it's not working
i change my ReqC2dmRegId.hbm.xml
to look like this (many-to-one):
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Mar 27, 2012 9:58:08 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping>
<class name="c2dm.ReqC2dmRegId" table="REQC2DMREGID">
<id name="id" type="long">
<column name="ID" />
<generator class="identity" />
</id>
<many-to-one name="person" class="Entities.Person" fetch="join" unique="true" cascade="save-update" not-null="true" >
<column name="PERSON" />
</many-to-one>
<property name="C2dmid" type="java.lang.String">
<column name="C2DMID" />
</property>
</class> </hibernate-mapping>
and this is working fine the problem is when i try to modify ReqC2dmRegId table
with my update method it create a now row with the same personid
id person_id C2dmid
1 3 asd123
2 3 dfvghj
way it's not update the right row instated create a new one and although i make the "many to one" property to be unique="true"?
thanks in advance
You have to be clear about the kind of relation: Is one-to-one or many-to-one?
Look like is a many-to-one unidirectional relation.
Here is an example with Annotations:
#Entity
#Table(name="PERSON")
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String _email;
private String _pass;
//getters and setters
}
And the other class:
#Entity
#Table(name="ReqC2dmRegId")
public class ReqC2dmRegId {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#ManyToOne
#JoinColumn(name = "PERSON_ID")
private Person person;
//getters and setters
}

Hibernate help me write query

Im making an email client in java. When user creates mail with attachment(document), it saves all the data about email message in database but in multiple tables, like attachement title in Document_table.title, number of message in msgnumber.num, date in msgnumber.date, name of sender in Official_Person.name and OfficialPerson.secondname.
How do i retrieve all this data and display it (im using Jtable for this)? I know how to get data if it saved in one table but not multiple. please help me.
one format has many documnets.
DOCUMENT:
#Entity
#Table(name="DOCUMENT"
,schema="Default"
)
public class Document implements java.io.Serializable {
#ManyToOne
#JoinColumn(name = "FormatID")
private Format format;
#Id
#Column(name = "DocumentID", unique = true, nullable = false)
private int documentId;
FORMAT :
#Entity
#Table(name="FORMAT"
,schema="Default"
)
public class Format implements java.io.Serializable {
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "FormatID")
private Set<Document> documents = new HashSet();
#Id
#Column(name = "FormatID", unique = true, nullable = false)
private int formatId;
format.hbm
<hibernate-mapping>
<class name="entity2.Format" table="FORMAT">
<id name="formatId" type="int">
<column name="FormatID" length="2000000000" />
<generator class="native" />
</id>
<set name="documents" table="DOCUMENT"
inverse="true" lazy="true" fetch="select">
<key>
<column name="FormatID" not-null="true" />
</key>
<one-to-many class="entity2.Document" />
</set>
document.hbm
<hibernate-mapping>
<class name="entity2.Document" table="DOCUMENT">
<id name="documentId" type="int">
<column name="DocumentID" length="2000000000" />
<generator class="native" />
</id>
<many-to-one name="format" class="entity2.Format" fetch="select">
<column name="FormatID" not-null="true" />
</many-to-one>
i want to retrieve all documents for format 1:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Format f = (Format) session.get(Format.class, 1);
System.out.println(f.getName());
System.out.println(f.getDocuments());
documents is empty? where am i wrong?
If you define a relationship between classes, for example:
class Person {
#OneToMany(cascade=CascadeType.ALL,
fetch= FetchType.EAGER)
private Set<Email> emails = new HashSet();
// getters/setters and some other attributes are not shown
When you read an object from the database, you will get another object that has a relationship with it automatically.
Session s = HibernateUtil.getSessionFactory().openSession();
Person p = (Person) s.get(Person.class, 1);
s.close();
System.out.println(p.getName());
System.out.println(p.getEmails());
The following is an example of bidirectional one to one relationship.
class Person {
#OneToOne(cascade=CascadeType.ALL)
private Address address;
class Address {
#OneToOne(mappedBy=”address”)
private Person person
Hibernate is an ORM tool - the "O" stands for "object". Start with an Email object that maps to your table and columns. Then use HQL to query for Email instances that meet a particular restriction.
JTable or web page are display issues that are separate from the manner in which you query for objects.
You just write the query using a select statement for all of the values you wish to retrieve. Hibernate will return an array with those values in it with the indices in the same order as your select statement.
SELECT
FROM Person AS P, Address AS A, Order AS O, User AS U
WHERE P.id = 5
AND A.personId = P.id
AND O.personId = P.id
AND U.personId = P.id
This will return an array containing {person, address, List, User}

Categories

Resources