简体   繁体   English

休眠-实体-数据库保存

[英]Hibernate - Entity - Database Saving

I am new to Hibernate and asked to work with a database with tables that has these columns 我是Hibernate的新手,并被要求使用具有这些列的表的数据库

Table : tbl_product //List of Inventory Items 表格:tbl_product //库存项目清单
Columns: 列:
key_product key_product
key_category key_category
fld_product_name fld_product_name
fld_inventory_qty fld_inventory_qty
fld_unit_price fld_unit_price
fld_product_image fld_product_image

Table: tbl_order_detail //Shopping Cart Columns: 表:tbl_order_detail //购物车列:
key_order_detail key_order_detail
key_order (reference to tbl_order.key_order) key_order(对tbl_order.key_order的引用)
key_product (reference to tbl_product.key_product) key_product(对tbl_product.key_product的引用)
fld_unit_price fld_unit_price
fld_quantity fld_quantity

Table tbl_order //Pivot table for Shopping Cart and User -- with total price of shopping cart 表tbl_order //购物车和用户的数据透视表-含购物车总价
Columns: 列:
key_order key_order
key_user key_user
fld_total_amount fld_total_amount

And my models (objects) looks like this: 我的模型(对象)如下所示:

Model: InventoryItem 型号:InventoryItem
Fields: 领域:
Product product; 产品产品;
int quantity; 整数数量

Model: Product 型号:产品
Fields: 领域:
int productKey; int productKey;
String name; 字符串名称;
Category category; 类别类别;
BigDecimal unitPrice; BigDecimal单价;
String productImage; 字符串productImage;

Model: Cart Item 型号:购物车项目
Fields: 领域:
Product product; 产品产品;
int quantity; 整数数量

I am faced with the problem as to how to map the tables with the corresponding models. 我面临着如何用相应模型映射表的问题。 I have seen that I can use @Embedded and @Embeddable but due to the restriction that I cannot modify the database, it seems difficult to do so. 我已经看到可以使用@Embedded和@Embeddable,但是由于不能修改数据库的限制,这样做似乎很困难。 Any help will be appreciated. 任何帮助将不胜感激。 Thank you! 谢谢!

Yes, of course it is possible, that's exactly what is Hibernate used for. 是的,当然有可能,这正是Hibernate的用途。 Start with Java Persistence API part of Java EE tutorial. 从Java EE教程的Java Persistence API部分开始。 This will show you how to map entities to database tables and how to query them. 这将向您展示如何将实体映射到数据库表以及如何查询它们。

http://docs.oracle.com/javaee/6/tutorial/doc/bnbpy.html http://docs.oracle.com/javaee/6/tutorial/doc/bnbpy.html

Please try the below. 请尝试以下。

Updated: 更新:

Mapping for table: tbl_product 映射表:tbl_product

@Entity
@Table(name="tbl_product")
public class Product {

    @Id
    @Column(name = "key_product")
    private int productKey;

    @Column(name = "fld_product_name")
    private String name;

    @Column(name = "fld_product_name")
    private BigDecimal unitPrice;

    @Column(name = "fld_product_image")
    private String productImage;

    @OneToOne(mappedBy="product")
    private InventoryItem inventoryItem;
}

@Entity
@Table(name="tbl_product")
public class InventoryItem {

    @Id
    @Column(name = "key_product")
    private int productKey;

    @Column(name = "fld_inventory_qty")
    private int quantity;

    @OneToOne
    @PrimaryKeyJoinColumn
    private Product product;
}

Mapping for tbl_order would be 映射tbl_order将是

@Entity
@Table(name="tbl_order")
public class ShoppingCartItem {

    @Id
    @Column(name="key_order")
    private int keyOrder;

    @Column(name="fld_total_amount")
    private BigDecimal totalAmount;

    //I guess this is a foreign key for User entity. I hope you can map it by yourself 
    @Column(name="key_user")
    private int keyUser;        

@OneToOne(mappedBy="shoppingCartItem")
private ShoppingCartItemDetails shoppingCartDetails;
} 

Mapping for table tbl_order_detail would be 表tbl_order_detail的映射为

@Entity
@Table(name="tbl_order_detail")
public class ShoppingCartItemDetails {

    @Id
    @Column(name="key_order_detail")
    private int keyOrderDetail;

    @Column(name="key_order", insertable=false, updatable=false)
    private int keyOrder;

    @OneToOne
    //@JoinColumn used to map foreign key with primary key
    @JoinColumn(name="key_order", referencedColumnName="key_order")
    private ShoppingCartItem shoppingCartItem;

    @Column(name="key_product", insertable=false, updatable=false)
    private int keyProduct;

    @OneToOne
    @JoinColumn(name="key_product", referencedColumnName="key_product")
    private Product product;

    @Column(name="fld_unit_price")
    private BigDecimal unitPrice;

    @Column(name="fld_quantity")
    private int quantity;   
}

您也可以使用xml映射,从这里开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM