close

本周上第三次課程

主要的部分 還是在Mapping的xml檔的設定

特別要提到 一對多 及 多對一 的xml設定

ex :

<many-to-one name="league" column="league_id" class="duke.soccer.model.League" fetch="select" >

</many-to-one>

而 Id 的部分 我們盡量定義為 Integer  ,

int 初始 為0

integer 可為 null  較方便

而切記 每新增一個mapping 的 xml 記得都要到 hibernate.cfg.xml 內去新增 resource 的路徑

ex:

<mapping resource="duke/soccer/model/Player.hbm.xml"  />

================================================

 //(configure)

 hibernate.cfg.xml:  

<hibernate-configuration>
<session-factory name="SessionFactory">
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/webimpl</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>
<mapping resource="duke/soccer/model/Player.hbm.xml" />
<mapping resource="duke/soccer/model/League.hbm.xml" />
<mapping resource="duke/soccer/model/Register.hbm.xml" />
</session-factory>
</hibernate-configuration>

========================================================

/////////    Mapping 

Register.java:

package duke.soccer.model;

public class Register {

private Integer id;
private League league;
private Player player;
private String division;

public Register() {
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public League getLeague() {
return league;
}

public void setLeague(League league) {
this.league = league;
}

public Player getPlayer() {
return player;
}

public void setPlayer(Player player) {
this.player = player;
}

public String getDivision() {
return division;
}

public void setDivision(String division) {
this.division = division;
}



//�в��ͩҦ��ݩʪ�getter/setter, id�ݩ�setter���׹��l�Чאּprivate
}

///////////////////////////////////////////////////////////////////////////////////////

register.hbm.xml:

<hibernate-mapping package="duke.soccer.model" >
<class name="Register" table="register">
<id name="id" type="java.lang.Integer" >
<column name="id"></column>
<generator class="native"></generator>
</id>

<property name="division" type="string">
<column name="division"></column>
</property>

<many-to-one name="league" column="league_id" class="duke.soccer.model.League" fetch="select" >

</many-to-one>

<many-to-one name="player" column="player_name" class="duke.soccer.model.Player" fetch="select" >
</many-to-one>

</class>
</hibernate-mapping>

 

/// fetch="select" 也可用 join 方式來查詢 ,但老師目前尚未搞清楚這塊;

----------------------------------------------------------

// 多對一    many-to-one

----------------------------------

//另外 一對多如下

再 league.hbm.xml下:

<set name="registers" table="register" fetch="select" lazy="false" inverse="false" >
<key column = "id" ></key>
<one-to-many class="duke.soccer.model.Register" />
</set>

然後呼叫時使用:

League l = (League) session.createQuery("from League where id=1").uniqueResult();
set<Register> registers = l.getRegisters();
for(Register r : registers)
{
System.out.println(r.getPlayer().getAddress());
}

// 再register.hbm.xml內

已經定義了 player 和 league 兩個物件 於是就可以利用以上的方式去取得player或league內的資訊

--------------------------------------------------------

 

=====================================================

(Entity)

EntityManager.java :

public static void main(String[] args) {   // 起始點

Register r = storeRegister(league,pp,"showSomething");
System.out.println( r.getDivision());

queryPlayerFrom();

 }

private static void queryPlayerFrom() {
Session sessionLink = HibernateUtils.getSessionFactory()
.getCurrentSession();
sessionLink.beginTransaction();
//Register

}

 

private static Register storeRegister(League l , Player p , String division1)
{
Register r = new Register();
r.setLeague(l);
r.setPlayer(p);
r.setDivision(division1);

Session sessionLink = HibernateUtils.getSessionFactory()
.getCurrentSession();
sessionLink.beginTransaction();
sessionLink.save(r);
sessionLink.getTransaction().commit();
return r;
}

 

===================================================

// 建立 SessionFactory

HibernateUtils.java:

 

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}

 

// 說明一下  Configuration().configure().buildSessionFactory();

//這行就是由 hibernate.cfg.xml 來的 由 import org.hibernate.cfg.Configuration; 

 

====================================================

開頭有說剛開始的時候 id值要盡量社為 Interger

所以在 equals 改寫上應如下方所示

public boolean equals(Object o) {
boolean result = false;
if ( o instanceof League ) {
League l = (League) o;
result = this.id.equals(l.id);
}
return result;
}

====================================================

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JoshS 的頭像
    JoshS

    JoshS的部落格

    JoshS 發表在 痞客邦 留言(0) 人氣()