zoukankan      html  css  js  c++  java
  • hibernate 多对一关联

    (转自尚学堂教学内容)

     

    注解多对一:

    package com.bjsxt.hibernate;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name="t_group")
    public class Group {
        private int id;
        private String name;
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

     

    package com.bjsxt.hibernate;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;

    @Entity
    @Table(name="t_user")
    public class User {
        private int id;
        private String name;
        private Group group;
        @ManyToOne
       
        public Group getGroup() {
            return group;
        }
        public void setGroup(Group group) {
            this.group = group;
        }
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

     

    xml多对一:

    Group.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>
        <class name="com.bjsxt.hibernate.Group" table="t_group">
            <id name="id">
                <generator class="native"></generator>
            </id>
           
            <property name="name"></property>
           
        </class>
       
    </hibernate-mapping>

     

    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>
        <class name="com.bjsxt.hibernate.User" table="t_user">
            <id name="id">
                <generator class="native"></generator>
            </id>
           
            <property name="name"></property>
            <many-to-one name="group" column="groupId" />
        </class>
       
    </hibernate-mapping>

     

     

    附:

    hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
    <!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="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
            <property name="connection.username">root</property>
            <property name="connection.password">bjsxt</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
           <!--
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property>
            <property name="connection.username">scott</property>
            <property name="connection.password">tiger</property>
              <property name="dialect">org.hibernate.dialect.OracleDialect</property>
           -->

            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>

           

            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>

            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>

            <!-- Drop and re-create the database schema on startup
            <property name="hbm2ddl.auto">update</property>
             -->
            <!-- 
            <mapping class="com.bjsxt.hibernate.Group"/>
            <mapping class="com.bjsxt.hibernate.User"/>
           
             -->
             <mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/>
             <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
           
        </session-factory>

    </hibernate-configuration>

  • 相关阅读:
    GoLang之网络
    GoLang之方法与接口
    GoLang之基础
    Twemproxy 缓存代理服务器
    判断点是否在三角形内
    C++中const 的各种用法
    解决java web中safari浏览器下载后文件中文乱码问题
    Spring MVC如何测试Controller(使用springmvc mock测试)
    java生成指定范围的随机数
    itextpdf添加非自带字体(例如微软雅黑)
  • 原文地址:https://www.cnblogs.com/flying607/p/3478239.html
Copyright © 2011-2022 走看看