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:
When you read an object from the database, you will get another object that has a relationship with it automatically.
The following is an example of bidirectional one to one relationship.