Hibernate annotation development and other level annotations

Hibernate's annotation categories can be divided into

Class-level annotations

Attribute-level annotation    

Notes on association mapping

First introduce the annotations at the class level

@Entity (mapping entity class annotation)

    @Entity(name = "The name of the table in the database")

    name: Optional, corresponding to a table in the database, if the table name is the same as the entity class name, it can be omitted.

    Need to specify the primary key attribute when using @Entity

example:

Configuration file hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/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://192.168.5.134:3306/hibernate_mysql</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property> 
        <!--Configuration dialect
        -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 
        <!--Whether to show sql statement 
        --> 
        <property name="show_sql">true</property> 
        <!--format sql 
        --> 
        <property name="format_sql">true</property> 
        <!--Table building strategy 
        --> 
        <property name="hbm2ddl.auto">update</property> 
        <!--If you are using local Transaction (jdbc transaction local transaction: there is only one database) 
        --> 
        <property name="hibernate.current_session_context_class">thread</property> 
        <!--If you are using a global transaction (jta transaction global transaction: there are multiple databases) 
        --> 
        <!--<property name="hibernate.current_session_context_class">jta</property>-->--<property name="hibernate.current_session_context_class">jta</property>--> 
        <!--The table corresponding to the created entity class
        -->
        <!--<mapping class="com.idea.hibernate.oto_bfk.Students05"/>-->
        <!--<mapping class="com.idea.hibernate.oto_bfk.IdCard02"/>-->

        <mapping class="com.idea.hibernate.mto_fk.Students06"/>
        <mapping class="com.idea.hibernate.mto_fk.ClassRoom"/>
    </session-factory>
</hibernate-configuration>

Entity class Students

@Entity(name = "Students") //映射成关系型数据库中的一张表
//javax.persistence.Entity ==> jpa注解
public class Students {

    @Id //设置为主键
    @GeneratedValue(strategy = GenerationType.AUTO) //主键自增
    private int sid; //学号
    private String sname; //姓名
    private String gender; //性别
    private Date birthday; //出生日期
    private String major; //专业
    private String address; //地址

    public Students() {
    }

    public Students(int sid, String sname, String gender, Date birthday, String major, String address) {
        this.sid = sid;
        this.sname = sname;
        this.gender = gender;
        this.birthday = birthday;
        this.major = major;
        this.address = address;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

 test

@Test
public void testSchemaExport(){
    //创建hibernate配置对象
    Configuration configuration = new Configuration().configure();
    //创建服务注册对象
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
    //生成SssionFactory
    SessionFactory  sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    //生成SchemaExport对象
    SchemaExport schemaExport = new SchemaExport(configuration);
    schemaExport.create(true, true);
}

Console output

Database results 

@Table (annotation of annotated table)

    @Table(name = "", catalog = "", schema = "")

    Generally used in conjunction with @Entity annotation, it is also used on classes

    name: Optional, the name of the mapping table, the default table name is the same as the entity name, and the table name needs to be specified only if it is inconsistent

    catalog: optional, represents the catalog directory name, default is empty

    schema: optional, represents the schema name, default is empty

    The difference between schema and catalog: A database system is composed of multiple catalogs, and a catalog is composed of multiple schemas ( mysql and oracle do not support catalog attributes ), the value of mysql's schema is the name of the database by default, and oracle is the user's id by default

@Entity
@Table(name = "t_students", schema = "hibernate_mysql")
//javax.persistence.Entity ==> jpa注解
public class Students {...}

Console output

Database results

@ Embeddable (note the annotation of the embedded class)

    Indicates that a non-Entity class can be embedded in another Entity class as a property

    Since it is not an Entity class, the marked class will not be mapped into a table

Create a new non-Entity class of Address and mark it with @Embeddable annotation

//地址类
@Embeddable  //表示这是一个非Entity的嵌入类, 该类在另一个实体类中充当属性
public class Address {

    private String postCode;  //邮编
    private String address;  //地址
    private String phone;  //联系电话

    public Address() {
    }

    public Address(String postCode, String address, String phone) {
        this.postCode = postCode;
        this.address = address;
        this.phone = phone;
    }

    public String getPostCode() {
        return postCode;
    }

    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

    

Database results

Guess you like

Origin blog.csdn.net/ip_JL/article/details/85679683