Java 休眠映射
我是新来的Java 休眠映射,java,mysql,hibernate,Java,Mysql,Hibernate,我是新来的hibernate并试图理解它。我有以下表格:- 我们有如下会话tbl表:- 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 |
hibernate
并试图理解它。我有以下表格:-
我们有如下会话tbl
表:-
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
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>
在上面的session\u id
用作session\u tbl
的外键
在hibernate
中,如何在xml
文件中指定映射?
我只想获取stock\u tbl
数据,我永远不会查询session\u tbl
来获取股票
我们需要在Stock\u tbl
类中创建Session\u tbl
对象。但是没有Stock\u tbl
对象与Session\u tbl
类
我们想使用session\u tbl
进行跟踪,我们有100多个表,它们在所有表中都有session\u tbl
作为外键。在这种情况下,我们必须为Session\u tbl
中的所有类设置不必要的设置。因为我们只需要Stock\u tbl
中的会话详细信息,其余99个表也一样(需要时获取session\u tbl
-延迟加载)
我已经创建了如下映射:-
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
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>
public class Session\u tbl{
专用int会话_id;
私有字符串日期时间;
私募股权;
//问题1
…接二连三
}
公共类股票{
私人国际股票代码;
非公开会议_tblsessionbl;
私人int价格;
…接二连三
}
//问题2
......
....
问题:
在Stock tbl类中只定义关系的多个部分。 如果您的100个类中的任何一个不想使用它们与会话的关系,那么您不需要向会话添加任何内容
<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>
除非你的意思是你真的根本不想上课。
那么就不提供任何会话映射。
在您的Stock\u tbl中,您只为session\u id定义了一列,您需要自己处理它(即设置正确的sessionId值)
将表格结构发布为CREATETABLE语句通常会使人们更容易回答。个人品味问题。当前格式的表结构足够清晰。谢谢您的回答。