Reputation: 747
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?
Upvotes: 0
Views: 282
Reputation: 12706
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
Upvotes: 1
Reputation: 15052
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}
Upvotes: 1
Reputation: 308763
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.
Upvotes: 1