zoukankan      html  css  js  c++  java
  • 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/

     

    宝贝网址:

  • 相关阅读:
    KVC笔记
    在iOS工程中引入C++静态库
    看了iOS 7和Xcode 5后的感想
    OpenGL学习第一天
    常用iOS游戏开发工具与SDK
    分享一个技巧,利用批处理调用ruby脚本(可能你为路径苦恼)
    ruby酷酷的方法——另一种next
    ruby的字符串性能到底如何最佳
    ruby元编程之 method_missing 一个细节
    ruby的继承到底可以继承哪些东西
  • 原文地址:https://www.cnblogs.com/W203654/p/2253106.html
Copyright © 2011-2022 走看看