zoukankan      html  css  js  c++  java
  • JPA中的一对多关系

    order.java

     1 import java.util.hashset;
     2 import java.util.set;
     3 
     4 import javax.persistence.cascadetype;
     5 import javax.persistence.column;
     6 import javax.persistence.entity;
     7 import javax.persistence.fetchtype;
     8 import javax.persistence.id;
     9 import javax.persistence.onetomany;
    10 import javax.persistence.table;
    11 
    12 @entity
    13 @table(name="orders")
    14 public class order {
    15     @id
    16     private string orderid;
    17     @column(nullable=false)
    18     private float amount=0f;
    19     @onetomany(cascade=cascadetype.all,fetch=fetchtype.lazy,mappedby="order")
    20     private set<orderitem> items=new hashset<orderitem>();
    21     public string getorderid() {
    22         return orderid;
    23     }
    24     public void setorderid(string orderid) {
    25         this.orderid = orderid;
    26     }
    27     public float getamount() {
    28         return amount;
    29     }
    30     public void setamount(float amount) {
    31         this.amount = amount;
    32     }
    33     public set<orderitem> getitems() {
    34         return items;
    35     }
    36     public void setitems(set<orderitem> items) {
    37         this.items = items;
    38     }
    39     
    40 }

    orderitem.java

     1 import javax.persistence.cascadetype;
     2 import javax.persistence.column;
     3 import javax.persistence.entity;
     4 import javax.persistence.generatedvalue;
     5 import javax.persistence.id;
     6 import javax.persistence.joincolumn;
     7 import javax.persistence.manytoone;
     8 
     9 @entity
    10 public class orderitem {
    11     @id
    12     @generatedvalue
    13     private integer id;
    14     @column(length=40,nullable=false)
    15     private string productname;
    16     @column(nullable=false)
    17     private float price=0f;
    18     @manytoone(cascade={cascadetype.merge,cascadetype.refresh},optional=false)
    19     @joincolumn(name="order_id")
    20     private order order;
    21     public integer getid() {
    22         return id;
    23     }
    24     public void setid(integer id) {
    25         this.id = id;
    26     }
    27     public string getproductname() {
    28         return productname;
    29     }
    30     public void setproductname(string productname) {
    31         this.productname = productname;
    32     }
    33     public float getprice() {
    34         return price;
    35     }
    36     public void setprice(float price) {
    37         this.price = price;
    38     }
    39     public order getorder() {
    40         return order;
    41     }
    42     public void setorder(order order) {
    43         this.order = order;
    44     } 
    45     
    46 }


  • 相关阅读:
    oracle11g expdp/impdp数据库
    SqlServer触发器
    tomcat8.5.20配置https
    oracle常用函数积累
    Eclipse 搭建tomcat+动态项目完整版
    Windows7下ftp服务器
    Orcle定时生成表数据作业
    Oracle将一列值逗号拼接wm_concat函数
    Oracle表空间 ORA-01653:
    node+mongodb+ionic+cordova
  • 原文地址:https://www.cnblogs.com/mrbourne/p/9959446.html
Copyright © 2011-2022 走看看