欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate annotation

程序员文章站 2022-04-17 10:49:57
...

 

hibernate annotation主要是利用JPA的接口使元数据注释代替xml映射文件。如:
Java代码  
import java.io.Serializable;  
import java.util.Date;  
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;  
import javax.persistence.GenerationType;  
import javax.persistence.Id;  
import javax.persistence.Table;  
@Entity  
@Table(name="user")  
public class User implements Serializable{  
    private static final long serialVersionUID = 1L;  
    private int id;  
    private int age;  
    private String name;  
    private String password;  
    @Id  
    //采用数据库自增方式  
    @GeneratedValue(strategy=GenerationType.AUTO)  
    //@GeneratedValue  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    public static long getSerialversionuid() {  
        return serialVersionUID;  
    }  
    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;  
    }  
}  
 hibernate.cfg.xml 配置
Java代码  
<hibernate-configuration>  
    <session-factory>  
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="hibernate.connection.url">jdbc:mysql:///test</property>  
        <property name="hibernate.connection.username">root</property>  
        <property name="hibernate.connection.password">root</property>  
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
        <property name="hibernate.hbm2ddl.auto">update</property>  
        <property name="hibernate.show_sql">true</property>  
        <property name="hibernate.format_sql">true</property>  
        <mapping class="com.linkhome.annotation.User" />  
    </session-factory>  
</hibernate-configuration>  
 
hibernate.cfg.xml的读取:
Java代码  
Configuration cfg = new AnnotationConfiguration().configure();  
 
多对一关系,一对多:
Java代码  
import java.io.Serializable;  
import java.util.HashSet;  
import java.util.Set;  
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;  
import javax.persistence.Id;  
import javax.persistence.JoinColumn;  
import javax.persistence.OneToMany;  
import javax.persistence.Table;  
@Entity  
@Table(name = "class")  
public class Class implements Serializable {  
    private static final long serialVersionUID = 1L;  
    private int id;  
    private String name;  
    private Set<Student> st=new HashSet<Student>();  
  
    @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;  
    }  
  
    @OneToMany(mappedBy="classes",targetEntity=Student.class)  
    @JoinColumn(name="classID")  
  
  
  
    public Set<Student> getSt() {  
        return st;  
    }  
  
    public void setSt(Set<Student> st) {  
        this.st = st;  
    }  
  
    public static long getSerialversionuid() {  
        return serialVersionUID;  
    }  
      
      
  
}  
 
 
Java代码  
import java.io.Serializable;  
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;  
import javax.persistence.Id;  
import javax.persistence.JoinColumn;  
import javax.persistence.ManyToOne;  
import javax.persistence.Table;  
@Entity  
@Table(name = "student")  
  
public class Student implements Serializable {  
    private static final long serialVersionUID = 1L;  
    private int id;  
    private String name;  
    private String password;  
    private Class classes;  
    @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;  
    }  
  
    public String getPassword() {  
        return password;  
    }  
  
    public void setPassword(String password) {  
        this.password = password;  
    }  
  
    public static long getSerialversionuid() {  
        return serialVersionUID;  
    }  
  
      
  
    @ManyToOne  
    @JoinColumn(name="classID")  
    public Class getClasses() {  
        return classes;  
    }  
  
    public void setClasses(Class classes) {  
        this.classes = classes;  
    }  
}