Hibernate初学の1対多、多対一関係モデル
例受注と顧客
複数対1の関係モデル:
・まず、ガイドパッケージ;これは省略して書きませんが、
・次にdomainクラスを書く
Client Orderの2つのクラスに分けられ、多対一の関係なので、外部キーはOrderというクラスに置くべきです.
プロファイル:hibernate.cfg.xml
複数対1の関係モデル:
・まず、ガイドパッケージ;これは省略して書きませんが、
・次にdomainクラスを書く
Client Orderの2つのクラスに分けられ、多対一の関係なので、外部キーはOrderというクラスに置くべきです.
プロファイル:hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="foo">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/hbsi/domain/Client.hbm.xml"/>
<mapping resource="com/hbsi/domain/Order.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Client ;
· ,Client.hbm.xml
<hibernate-mapping package=”com.hbsi.domain”>
<class name=”Client” table=”client”>//
<id name=”id” column=”id”>//
<generator class= ”native” />//
</id>
<property name=”name” />
</class>
</hibernate -mapping>
Order.hbm.xml
<hibernate-mapping>
<class name=”Order” table=”ordershop”>
<id name=”id”>
<generator class=”native” />
</id>
<property name=”shopname” column=”shopname” />
<property name=”number” column=”number”/>
<property name = “price” column=”price” />
<many-to-one name=”client” column=”client_id” />
<class>
</hibernate-mapping>
, ;
Session session=null;
@Test
public void add(Client client,Order order){
try{
session = HibernateUtil.getSession();//
session.beginTransaction();//
/*Client client = new Client();
client.setName(" ");
Order order1 = new Order();
order1.setShopname(" ");
order1.setNumber(2);
order1.setPrice(22);
order1.setCt(client);
Order order2 = new Order();
order2.setShopname(" ");
order2.setNumber(2);
order2.setPrice(22);
order2.setCt(client);*/
session.save(client);
session.save(order);
session.beginTransaction().commit();
}finally{
if(session!=null){
session.close();
}
}
}
public void find(int id){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
Order order = (Order) session.get(Order.class, id);
System.out.println(order.getCt().getName());
session.beginTransaction().commit();
}finally{
if(session!=null){
session.close();
}
}
}
:
public static void main(String[] args ){
Client client = new Client();
client.setName("xiao ");
Order order1 = new Order();
order1.setShopname(" ");
order1.setNumber(2);
order1.setPrice(22);
order1.setCt(client);
Order order2 = new Order();
order2.setShopname(" ");
order2.setNumber(2);
order2.setPrice(22);
order2.setCt(client);
Many2one mo = new Many2one();
mo.add(client, order1);
mo.find(2);
}
:
Client.hbm.xml
<hibernate-mapping package="com.hbsi.domain2">
<class name="Client" table="client">
<id name="id" column="id">
<generator class = "native" />
</id>
<property name="name" />
<set name="order" table="ordershop" cascade="save-update">
<key column="user_id" />
<one-to-many class="Order" />
</set>
</class>
</hibernate-mapping>
Order.hbm.xml
<hibernate-mapping package="com.hbsi.domain2">
<class name="Order" table="ordershop">
<id name="id">
<generator class = "native" />
</id>
<property name="shopname" column="shopname" />
<property name="number" column="number" />
<property name="price" column="price" />
</class>
</hibernate-mapping>
:
public void add(Client client,Order order){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
session.save(client);
// session.save(order);
session.beginTransaction().commit();
}finally{
if(session!=null){
session.close();
}
}
}
public void find(int id){
try{
session = HibernateUtil.getSession();
session.beginTransaction();
Client client = (Client) session.get(Order.class, id);
Set<Order> orders =client.getOrder();
orders.size();
session.beginTransaction().commit();
}finally{
if(session!=null){
session.close();
}
}
}