其他分享
首页 > 其他分享> > 《Hibernate上课笔记》-----class3

《Hibernate上课笔记》-----class3

作者:互联网

目录

一:单实体的对象标识符映射

Hibernate采用对象标识符(OID)区分对象。

<class name="User" table="t_user">
        <id name="id" type="int" >
           <column name="id"></column>
           <generator class="identity"></generator>
        </id>
</class>

1.increment关键字

<class name="User" table="t_user">
        <id name="id" type="int" >
           <column name="id"></column>
           <generator class="increment"></generator>
        </id>
        <property name="userName" column="user_name" type="java.lang.String">
        </property>
       <property name="passWord" type="java.lang.String"></property>      
    </class>

2.identity关键字

 <class name="User" table="t_user">
        <id name="id" type="int" >
           <column name="id"></column>
           <generator class="identity"></generator>
        </id>
        <property name="userName" column="user_name" type="java.lang.String">
        </property>
       <property name="passWord" type="java.lang.String"></property>      
    </class>

3.assigned关键字

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.entity">
    <class name="User" table="t_user">
        <id name="id" type="int" >
           <column name="id"></column>
           <generator class="assigned"></generator>
        </id>
        <property name="userName" column="user_name" type="java.lang.String">
        </property>
       <property name="passWord" type="java.lang.String"></property>      
    </class>
</hibernate-mapping>

二:使用注解映射单实体

作用:代替配置文件,直接在实体类里声明

@Formula(value="(select * from student)")

示例:

package com.hibernate.entity;

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

import org.hibernate.annotations.Formula;
import org.hibernate.annotations.GenericGenerator;



@Entity
@Table(name="t_user")
public class User {
	private Integer id;
	private String userName;
	private String passWord;
	@Id
	@GeneratedValue(generator="generator_increment")
	@GenericGenerator(name="generator_increment",strategy="increment")
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(name="user_name")
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	@Column(name="password")
	public String getPassWord() {
		return passWord;
	}
	@Formula(value="(select * from student)")
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", passWord=" + passWord + "]";
	}
	

}

最后小声bb:这个课可能已经传承了10几年了.哭

标签:Hibernate,name,OID,id,public,-----,increment,class3,String
来源: https://blog.csdn.net/qq_44867340/article/details/114406938