Hibernate之嵌套组件映射

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 |
+----------+-------+-------+-------+-------+-------+--------+
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值