• 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 }


  • 相关阅读:
    RUNOOB.COM-python网络编程-(python3.5.0)
    windows查看服务
    计算机网络里的一些理解
    如果面试有傻逼问道oracle怎么启动的
    推荐一个学习数据库的地方
    电脑中的驱动程序是什么,是干什么的
    Raspberry Pi 4B 之 Python开发
    Ubuntu20.04+EdgexFoundry边缘计算微服务搭建-----遇到的问题-----make build 被墙问题
    Raspberry Pi 4B + Ubuntu 20.04 server for arm64 的wifi配置
    关于PicoNeo开发环境的Unity3D+AndroidSDK配置
  • 原文地址:https://www.cnblogs.com/mrbourne/p/9959446.html
Copyright © 2020-2023  润新知