• JPA学习很好的学习手册


    今天发现一个不错的JPA学习博客,推荐给大家,很受用

    JPA学习笔记四[JPQL] 

    2011-04-27 10:24:13|  分类: 默认分类 |  标签: |字号订阅

     
     

    package org.itfuture.examples.domain;

     

    import java.util.HashSet;

    import java.util.Set;

     

    import javax.persistence.CascadeType;

    import javax.persistence.Entity;

    import javax.persistence.FetchType;

    import javax.persistence.GeneratedValue;

    import javax.persistence.Id;

    import javax.persistence.OneToMany;

    import javax.persistence.Table;

     

    @Entity

    @Table(name = "orders")

    public class Order {

           private Integer orderId;

           private Float amount = 0f;

           private Set<OrderItem> items = new HashSet<OrderItem>();

     

           @Id

           @GeneratedValue

           public Integer getOrderId() {

                  return orderId;

           }

     

           public void setOrderId(Integer orderId) {

                  this.orderId = orderId;

           }

     

           public Float getAmount() {

                  return amount;

           }

     

           public void setAmount(Float amount) {

                  this.amount = amount;

           }

     

           @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,

                         CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.LAZY , mappedBy="order")

    // many 的一方不会被立即加载,oeder参考方是orderItem 的oeder 属性,它自己不维护这个属性

           public Set<OrderItem> getItems() {

                  return items;

           }

     

           public void setItems(Set<OrderItem> items) {

                  this.items = items;

           }

     

    }

    package org.itfuture.examples.domain;

     

    import javax.persistence.CascadeType;

    import javax.persistence.Entity;

    import javax.persistence.GeneratedValue;

    import javax.persistence.Id;

    import javax.persistence.JoinColumn;

    import javax.persistence.ManyToOne;

    import javax.persistence.Table;

     

    @Entity

    @Table(name="order_items")

    public class OrderItem {

           private Integer id;

           private String productName;

           private Float sellPrice = 0f;

           private Order order;

     

           @Id

           @GeneratedValue

           public Integer getId() {

                  return id;

           }

     

           public void setId(Integer id) {

                  this.id = id;

           }

     

           public String getProductName() {

                  return productName;

           }

     

           public void setProductName(String productName) {

                  this.productName = productName;

           }

     

           public Float getSellPrice() {

                  return sellPrice;

           }

     

           public void setSellPrice(Float sellPrice) {

                  this.sellPrice = sellPrice;

           }

     

           @ManyToOne(cascade={CascadeType.REFRESH} , optional=false)

           @JoinColumn(name="orderId")// optional 是否是可选的,orderId对应数据库oeders

    //的一个字段

           public Order getOrder() {

                  return order;

           }

     

           public void setOrder(Order order) {

                  this.order = order;

           }

    }

    2.5.2        一对一双向映射

    package org.itfuture.examples.domain;

     

    import javax.persistence.CascadeType;

    import javax.persistence.Entity;

    import javax.persistence.GeneratedValue;

    import javax.persistence.Id;

    import javax.persistence.JoinColumn;

    import javax.persistence.OneToOne;

    import javax.persistence.PrimaryKeyJoinColumn;

    import javax.persistence.Table;

    @Entity

    @Table(name="persons")

    public class Person {

          

           private Integer id;

          

           private String name;

          

           private IdCard idCard;

     

           @Id

           //@GeneratedValue

           public Integer getId() {

                  return id;

           }

     

           public void setId(Integer id) {

                  this.id = id;

           }

     

           public String getName() {

                  return name;

           }

     

           public void setName(String name) {

                  this.name = name;

           }

    //方案一 :绿色

           //@OneToOne(cascade=CascadeType.ALL , optional=false)

           //@JoinColumn(name="idcardId" , unique=true)

    //方案二: 黄色 //共享主键

           @OneToOne(cascade=CascadeType.ALL)

           @PrimaryKeyJoinColumn

           public IdCard getIdCard() {

                  return idCard;

           }

     

           public void setIdCard(IdCard idCard) {

                  this.idCard = idCard;

           }

     

    }

    package org.itfuture.examples.domain;

     

    import javax.persistence.CascadeType;

    import javax.persistence.Entity;

    import javax.persistence.GeneratedValue;

    import javax.persistence.Id;

    import javax.persistence.OneToOne;

    import javax.persistence.Table;

     

    @Entity

    @Table(name="idcards")

    public class IdCard {

       

          

           private Integer id;

           private String cardNo;

           private Person person;

     

           @Id

           @GeneratedValue

           public Integer getId() {

                  return id;

           }

     

           public void setId(Integer id) {

                  this.id = id;

           }

     

           public String getCardNo() {

                  return cardNo;

           }

     

           public void setCardNo(String cardNo) {

                  this.cardNo = cardNo;

           }

     

           @OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST,

           CascadeType.REFRESH } , mappedBy="idCard" )

           public Person getPerson() {

                  return person;

           }

     

           public void setPerson(Person person) {

                  this.person = person;

           }

     

    }

    地址:http://blog.163.com/nyl5566@yeah/blog/static/63162237201132710241399/

     

    宝贝网址:

  • 相关阅读:
    网易云服务器使用https
    网站搬家后firefox,chrome提示内容编码错误,360浏览器提示500错误
    腾讯云centos服务器配置
    微擎二次开发相关
    mysql 分组查询最新的/最高的一条记录
    .htaccess ci放在子目录时规则
    小程序 html解析
    源于一道文字游戏的题目
    C# 获取系统时间及时间格式
    操作bin目录下的文件
  • 原文地址:https://www.cnblogs.com/W203654/p/2253106.html
Copyright © 2020-2023  润新知