简体   繁体   中英

Hibernate - Entity - Database Saving

I am new to Hibernate and asked to work with a database with tables that has these columns

Table : tbl_product //List of Inventory Items
Columns:
key_product
key_category
fld_product_name
fld_inventory_qty
fld_unit_price
fld_product_image

Table: tbl_order_detail //Shopping Cart Columns:
key_order_detail
key_order (reference to tbl_order.key_order)
key_product (reference to tbl_product.key_product)
fld_unit_price
fld_quantity

Table tbl_order //Pivot table for Shopping Cart and User -- with total price of shopping cart
Columns:
key_order
key_user
fld_total_amount

And my models (objects) looks like this:

Model: InventoryItem
Fields:
Product product;
int quantity;

Model: Product
Fields:
int productKey;
String name;
Category category;
BigDecimal unitPrice;
String 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. Any help will be appreciated. Thank you!

Yes, of course it is possible, that's exactly what is Hibernate used for. Start with Java Persistence API part of Java EE tutorial. 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

Please try the below.

Updated:

Mapping for table: 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

@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

@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映射,从这里开始

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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