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;
}
}
推荐阅读
-
hibernate与mysql进行大文件的存储实例教程
-
使用Hibernate Validator来帮你做数据校验
-
JQuery+Ajax+Struts2+Hibernate框架整合实现完整的登录注册
-
基于Struts2+Hibernate开发小区物业管理系统 源码 B
-
springboot使用hibernate validator校验,Bean Validation校验
-
Hibernate 框架入门
-
Intellij IDEA 2019 + Java Spring MVC + Hibernate学习笔记(1)
-
oracle数据库查询没问题,正常显示两个汉字;但是java后台Hibernate查询的时候只显示第一个汉字怎么解决?
-
java,hibernate和sqlserver对应的数据类型表
-
Hibernate无法提取结果集