Hibernate允许嵌套组件,比如:
进行如下的测试:
Good.java:
import javax.persistence.Embeddable;
@Embeddable
public class Good {
private String name;
private String model;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Item.java:
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
@Embeddable
public class Item {
private Good good;
private int count;
private double total;
@Embedded
public Good getGood() {
return good;
}
public void setGood(Good good) {
this.good = good;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
}
Order.java:
import java.util.List;
import javax.persistence.*;
@Entity
@Table(name = "tb_order")
public class Order {
private long id;
private List<Item> items;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ElementCollection(fetch = FetchType.LAZY, targetClass = Item.class)
@CollectionTable(name = "tb_item_order")
@OrderColumn(name = "itemID")
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
配置文件hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class"> com.mysql.jdbc.Driver </property>
<property name="connection.url"> jdbc:mysql://localhost:3306/db_test </property>
<property name="connection.username"> root </property>
<property name="connection.password"> 0000 </property>
<property name="dialect"> org.hibernate.dialect.MySQL5Dialect </property>
<property name="show_sql"> true </property>
<property name="hibernate.hbm2ddl.auto"> update </property>
<property name="connection.pool_size"> 16 </property>
<property name="current_session_context_class"> thread </property>
<mapping class="Order"/>
</session-factory>
</hibernate-configuration>
测试代码:
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class NestedEmbeddableTestDrive {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Good good1 = new Good();
good1.setName("good1");
good1.setModel("big");
good1.setPrice(99);
Item item1 = new Item();
item1.setGood(good1);
item1.setCount(5);
item1.setTotal(495);
Good good2 = new Good();
good2.setName("good2");
good2.setModel("mid");
good2.setPrice(98);
Item item2 = new Item();
item2.setGood(good2);
item2.setCount(10);
item2.setTotal(980);
Order order = new Order();
List<Item> items = new ArrayList<>();
items.add(item1);
items.add(item2);
order.setItems(items);
session.persist(order);
transaction.commit();
session.close();
}
}
数据库中显示如下:
mysql> select * from tb_order;
+----+
| id |
+----+
| 12 |
+----+
mysql> select * from tb_item_order;
+----------+-------+-------+-------+-------+-------+--------+
| Order_id | count | model | name | price | total | itemID |
+----------+-------+-------+-------+-------+-------+--------+
| 12 | 5 | big | good1 | 99 | 495 | 0 |
+----------+-------+-------+-------+-------+-------+--------+
| 12 | 10 | mid | good2 | 98 | 980 | 1 |
+----------+-------+-------+-------+-------+-------+--------+