zoukankan      html  css  js  c++  java
  • hibernate 学习知识总结

    1.最近用hibernate 学会很多知识,总结如下:

    (1)数据库表格已经设置默认值,在进行数据插入的时候,bean里面不赋值的话,插入之后该字段依旧是null

    是因为hibernate默认插入和更新所有字段,如果某些字段不需要操作,需要配置下
    (i)xml文件配置方式:
    <property name="account" type="java.lang.String" insert="false">
         <column name="account" length="100" />
    </property>

    加入insert="false" 就可以使它插入时不再对该字段进行操作,此值默认为true。同理update

    (ii)注释方式:

    @Column(insertable=false)
     public Integer getOrderNo() {
      return orderNo;
     }
     public void setOrderNo(Integer orderNo) {
      this.orderNo = orderNo;
     }

    以下是他人总结内容(原帖地址:http://blog.csdn.net/xzknet/article/details/3905741)

      1)<property>元素 insert属性:设置为false,在insert语句中不包含这个字段,表示永远不会被插入,默认true
      2)<property>元素 update属性:设置为false,在update语句中不包含这个字段,表示永远不会被修改,默认true
      3)<class>元素 mutable属性:设置为false就是把所有的<property>元素的update属性设置为了false,说明这个对象不会被更新,默认true
      4)<property>元素 dynamic-insert属性:设置为true,表示insert对象的时候,生成动态的insert语句,如果这个字段的值是null就不会加入到insert语句当中.默认false
      5)<property>元素 dynamic-update属性,设置为true,表示更新一个对象时,会生成动态SQL,当属性值发生变化时,才会包含到UPDATE语句中。 默认false  
      <class name="com.teccore.wjk.entity.Account" table="account" dynamic-insert="true" dynamic-update="true">
      6)<class>元素 dynamic-insert属性:设置为true,表示把所有的<property>元素的dynamic-insert属性设置为true,默认false
      7)<class>元素 dynamic-update属性:设置为true,表示把所有的<property>元素的dynamic-update属性设置为true,默认false
     
    (2)Hibernate Criterion设置查询条件
    Criterion是Criteria的查询条件.Criteria提供了add(Criterion criterion)方法来添加查询条件
    Criterion接口的主要实现包括:Example、Junction和SimpleExpression.
    (i)Junction的实际使用是它的两个子类conjunction和disjunction,分别是使用AND和OR操作符来联结查询条件集合.
    Disjunction和Conjunction是逻辑或和逻辑与,可以用这个来构造复杂的SQL查询条件,例子如下(来源:http://blog.csdn.net/whqcfp/article/details/6029061):
    Disjunction disjunction = Restrictions.disjunction();
      Criterion cirterion = Restrictions.sqlRestriction("SIMULPORTCAPACITY<SIMULPORTCAPACITYOCUPIED".toLowerCase());
      disjunction.add(cirterion);
      cirterion = Restrictions.sqlRestriction("ADSLPORTCAPACITY<ADSLPORTCAPACITYOCCUPIED".toLowerCase());
      disjunction.add(cirterion);
      cirterion = Restrictions.sqlRestriction("LANPORTCAPACITY<LANPORTCAPACITYOCCUPIED".toLowerCase());
      disjunction.add(cirterion);
      
      // ONU端口,至少要录入一种端口
      Conjunction conjunction = Restrictions.conjunction();
      cirterion = Restrictions.eq("lanportcapacity", 0);
      conjunction.add(cirterion);
      cirterion = Restrictions.eq("simulportcapacity", 0);
      conjunction.add(cirterion);
      cirterion = Restrictions.eq("adslportcapacity", 0);
      conjunction.add(cirterion);
    
      disjunction.add(conjunction);
      queryCriteria.add(disjunction);
     }

    构造出的sql语句如下:

    select *
      from aaaa this_
     where (simulportcapacity < simulportcapacityocupied or
           adslportcapacity < adslportcapacityoccupied or
           lanportcapacity < lanportcapacityoccupied or
           (this_.LANPORTCAPACITY = ? and this_.SIMULPORTCAPACITY = ? and
           this_.ADSLPORTCAPACITY = ?))
    (ii)SimpleExpression可以通过Restrictions工具类来创建,Restrictions提供了大量的静态方法;如:eq(等于)、ge(大于等于)、between等来方法的创建Criterion查询条件,使用Criteria进行查询,主要要清晰的是Hibernate提供了那些类和方法来满足开发中查询条件的创建和组装。
    List cats = sess.createCriteria(Cat.class)
    .add(Restrictions.like("name","Fritz%"))
    .add(Restrictions.between("weight",minWeight,maxWeight))
    .list();

    可以使用org.hibernate.criterion.Order来为查询结果排序.

    List cats = sess.createCriteria(Cat.class)
    .add(Restrictions.like("name","F%")
    .addOrder(Order.asc("name"))
    .addOrder(Order.desc("age"))
    .setMaxResults(50)
    .list();

    2.hibernate 注解方式(部分代码来源:http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html,附带文档链接

    英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

    中文:http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/

    )

    (1)实体类注解

    //当前的类是一个持久化类,是FeedBackEty这个类。他映射了一个表t_feedback。
    @Entity
    @Table(name = "t_feedback")
    public class FeedBackEty{
    //…………
    }

    (2)类属性注解:

    //指定主键生成策略,以后主键按此name生成
    @GenericGenerator(name = "generator", strategy = "increment")
    //指定主键
    @Id
    @GeneratedValue(generator = "generator")
    //指定该属性对应的数据库表格字段 是否唯一 和 不为空
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    //映射表中name这个字段 ,长度是500
    @Column(name = "name", length = 500)
    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

     (3)many to one  / one to many

    Product.java  Category.java为例

    多个product 可以对应一个category

    Category.java

    private Set<Product> products = new HashSet<Product>(0);
    //级联操作:cascade = CascadeType.ALL
    //延迟加载:fetch = FetchType.LAZY
    //映射:mappedBy = "category"
    //一对多方式
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "category")
    public Set<Product> getProducts() {
        return this.products;
    }
    
    public void setProducts(Set<Product> products) {
        this.products = products;
    }

    Product.java 

    private Category category;
    //延迟加载:多对一方式
    //关联信息:外键name = "category_id"
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    public Category getCategory() {
        return this.category;
    }
    
    public void setCategory(Category category) {
        this.category = category;
    }

    附加:

    //用于注释pojo对象中的属性,被注释的属性将成为短暂的,不会持久化到数据库的“短暂”属性。也就是说实体类中可以有此属性用于赋值获取值,但是表格里面没有这个对应字段
    @Transient
    public Date getInsertDate() {
            return insertDate;
        }
        public void setInsertDate(Date insertDate) {
            this.insertDate = insertDate;
        }
  • 相关阅读:
    OSCP Learning Notes Buffer Overflows(3)
    OSCP Learning Notes Buffer Overflows(5)
    OSCP Learning Notes Exploit(3)
    OSCP Learning Notes Exploit(4)
    OSCP Learning Notes Exploit(1)
    OSCP Learning Notes Netcat
    OSCP Learning Notes Buffer Overflows(4)
    OSCP Learning Notes Buffer Overflows(1)
    OSCP Learning Notes Exploit(2)
    C++格式化输出 Learner
  • 原文地址:https://www.cnblogs.com/cuiyf/p/3704567.html
Copyright © 2011-2022 走看看