Reputation: 1204
I am new to hibernate
and trying to understand it. I have below tables :-
We have session_tbl
table as below :-
Session_ID | Creation_Time
1 | 23-Mar-2014
2 | 24-Mar-2014
3 | 25-Mar-2014
4 | 26-Mar-2014
and stock_tbl as below :-
Stock_id | price | session_id
1 | 11.00 | 1
2 | 12.00 | 1
3 | 43.00 | 1
4 | 51.00 | 2
5 | 85.00 | 4
6 | 61.00 | 3
7 | 66.00 | 2
8 | 81.00 | 1
in above session_id
is using as foreign key for session_tbl
.
In hibernate
how can we specify mapping in xml
file ?
I want to fetch stock_tbl
data only, i will never query session_tbl
to get stock.
We need Session_tbl
object need to be created in Stock_tbl
class. But no Stock_tbl
object with Session_tbl
class.
We want to use session_tbl
for tracking purpose we have more then 100 table which are having session_tbl
as foreign key in all the table. in this case we have to put Set for all the classes in Session_tbl
unnecessary. As we want session details only from Stock_tbl
and same with all remain 99 tables (when required fetch session_tbl
- lazy loading).
I have created mapping as below :-
public class Session_tbl {
private int session_id;
private String dateTime;
private Set<Stock_tbl> stockTbl;
// Question 1
.. Getter and Setter
}
public class Stock_tbl{
private int stock_id;
private Session_tbl sessionTbl;
private int price;
.. Getter and Setter
}
<class name="Session_tbl" table="session_tbl">
<id name="session_id" type="int" column="session_id">
<generator class="native"/>
</id>
<set name="stockTbl" cascade="all">
<key column="stock_id"/>
<one-to-many class="Stock_tbl"/>
</set>
// Question 2
......
</Class>
<class name="Stock_tbl" table="stock_tbl">
<id name="stock_id" type="int" column="stock_id">
<generator class="native"/>
</id>
....
</class>
Question:
Why do i need to use set in Session_tbl class because i dont want to fetch stock from here? i need only Session_tbl Object in below class.
Why this mapping needed in Sesison_tbl table? for all the 100 classes do i have to create there mapping here?
Upvotes: 1
Views: 63
Reputation: 8552
You define only the ManyToOne part of the relationship in your Stock_tbl class. You don't need to add anything to the Session_tbl class, or any other of your 100 classes if they don't want to use their relationsip to the Session.
<class name="Stock_tbl" table="stock_tbl">
<id name="stock_id" type="int" column="stock_id">
<generator class="native"/>
</id>
<many-to-one name="sessionTbl" column="session_id" class="Session_tbl" not-null="true"/>
....
</class>
<class name="Session_tbl" table="session_tbl">
<id name="session_id" type="int" column="session_id">
<generator class="native"/>
</id>
//remove the stockTbl field from the Session class
</Class>
With anotation (recommended instead of xml as the mappings is next to your code), it simply looks like that:
@Entity
public class Stock_tbl{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int stock_id;
@ManyToOne
@JoinColumn(name="session_id")
private Session_tbl sessionTbl;
private int price;
.. Getter and Setter
}
@Entity
public class Session_tbl {
@GeneratedValue(strategy = GenerationType.AUTO)
private int session_id;
private String dateTime;
//remove the stock if you dont want to use it
.. Getter and Setter
}
Unless you meant that you really don't want to have the Session class at all. Then you don't provide any Session_tbl mapping. Inside your Stock_tbl you defined just the column for session_id, and you will need to handle it yourself (ie. set the correct sessionId value).
@Entity
public class Stock_tbl{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int stock_id;
private int session_id; //now it is a simple id, not relationships/objectc
private int price;
.. Getter and Setter
}
Upvotes: 1