zoukankan      html  css  js  c++  java
  • Hibernate基础映射

     在说Hibernate映射前,我们先来了解下对象关系映射 ORMORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对这些对象的操作。我们来看一张图

    通过该图,我们可以看出业务实体,在数据库中表现为关系数据,而在内存中表现为对象。应用程序处理对象很容易,但是很难处理关系数据。ORM做到了关系数据和对象数据之间的映射,可以通过映射关系自动产生SQL语句,在业务逻辑层和数据层之间充当桥梁。

    Hibernate映射

     

    Hibernate文件

    1.  映射类(*.java):它是描述数据库表的结构,表中的字段在类中被描述成属性,将来就可以实现把表中的记录映射成为该类的对象了。
    2.  映射文件(*.hbm.xml):它是指定数据库表和映射类之间的关系,包括映射类和数据库表的对应关系、表字段和类属性类型的对应关系以及表字段和类属性名称的对应关系等。
    3.  数据库配置文件(*.properties/*.cfg.xml):它是指定与数据库连接时需要的连接信息,比如连接哪种数据库、登录数据库的用户名、登录密码以及连接字符串等。当然还可以把映射类的地址映射信息放在这里。

    基本映射:

    具体看操作

    1映射实体类

    //默认空构造函数的重要性
    public class User {
    
        public User() {
            // TODO Auto-generated constructor stub
        }
        
        public User(String id,String name){
            this.id=id;
            this.name=name;
            
        }
        private String id;
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public Date getCreateTime() {
            return createTime;
        }
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
        public Date getExprieTime() {
            return exprieTime;
        }
        public void setExprieTime(Date exprieTime) {
            this.exprieTime = exprieTime;
        }
    
        private String name;
        private String password;
        private Date createTime;
        private Date exprieTime;
    
    }

    实体类的设计原则:

    * 实现无参的默认的构造函数

    * 提供一个标识

    *建议不要使用final修饰实体类(因为采用load延时加载数据的时候会继承实体类生成代理对象)

    *建议为实体类生成getter和setter方法(如果不使用,需要用属性field标识)

    2映射文件User.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
     <!--1、class和table的映射,name属性是实体名,table属性是表名(table可省略,则name即是映射的表名)--> 
        <class name="com.bjpowernode.hibernate.User">
        <!-2、主键映射,name属性是实体类的标识符属性,对应table的主键,即用column表示(column同样可省略)-->  
            <id name="id" access="field">
                <!--主键生成器,class属性表示生成策略,根据不同的需求选择-->   
                <generator class="uuid"/>
            </id>
             <!--3、其他属性的映射 property-->  
            <property name="name" length="40" unique="true" />
            <property name="password"/>
            <property name="createTime"/>
            <property name="exprieTime"/>
            <filter name="testFiltere"  condition="id < :myid"></filter>
        </class>
    </hibernate-mapping>

    3hibernate.cfg.xml配置文件

        <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
        
        <hibernate-configuration>
            <session-factory>
                <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernast_test</property>
                <property name="hibernate.connection.username">root</property>
                <property name="hibernate.connection.password">hanhan</property>
                <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
                <!--打印sql-->
                <property name="hibernate.show_sql">true</property>
                <!--在没有表的时候,创建sessionfactroy 时,就会去创建表(update的方式,不删除原有数据)-->
                <property name="hibernate.hbm2ddl.auto">update</property>
                
            <mapping  resource="com/bjpowernode/hibernate/User.hbm.xml"/>
            </session-factory>
        </hibernate-configuration>

    Hibernate的主键生成策略种类

    手动Assigned

    Hibernate主动:uuid

    数据库交互:

    需要和数据库交互以生成id的:guid、identity、sequence、native、foreign

    说明:需要和数据库交互生成,需要经过一次查询才能生成

    Guid,identityMySQLSQLserver的生成方式

    sequenceOracle,db2的生成方式,自增序列

    native:identity+sequence,跨平台

    foreign:只适用基于共享主键的一对一关联映射的时候使用。即一个对象的主键是参照的另一张表的主键生成的。 

    总结:

    Hibernate的基本映射:重点是对主键生成策略的认识,根据不同数据库选择不同的方式,重要理解。

  • 相关阅读:
    struts2 CVE-2014-0050(DoS), CVE-2014-0094(ClassLoader manipulation) S2-20 DoS attacks and ClassLoader manipulation
    struts2 CVE-2013-4316 S2-019 Dynamic method executions Vul
    struts2 CVE-2013-2251 S2-016 action、redirect code injection remote command execution
    struts2 CVE-2013-1965 S2-012 Showcase app vulnerability allows remote command execution
    struts2 CVE-2012-0392 S2-008 Strict DMI does not work correctly allows remote command execution and arbitrary file overwrite
    struts2 Advanced Learning
    2015阿里安全峰会
    PHPCMS phpsso_serverphpcmsmodulesphpssoindex.php、apiget_menu.php Authkey Leakage
    CMSEASY /lib/tool/front_class.php、/lib/default/user_act.php arbitrary user password reset vulnerability
    QTVA-2015-198545、WooYun-2015-104148 .NET Framework Arbitrary File Permissions Modify Vul
  • 原文地址:https://www.cnblogs.com/dyllove98/p/4085843.html
Copyright © 2011-2022 走看看