您的位置:首页 > 其它

Hibernate注解-类与属性

2018-02-01 22:10 323 查看
hibernate 注解分类

hibernate里常用注解包括,类注解,属性注解,关系注解,其他的注解

本知识点讲解类注解和属性注解
类注解

在注解示例-注解方式的Product中,Product类声明前面有两个注解:@Entity 和 @Table(name = "product_")

@Entity 表示这是一个实体类,用于映射表

@Table(name = "product_") 表示这是一个类,映射到的表名:product_
@Entity
@Table(name = "product_")
public class Product {
int id;
String name;
float price;
}

属性注解

然后是属性注解,属性注解是配置在属性对应的getter方法上的
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int getId() {
return id;
}
@Id 表示这是主键

@GeneratedValue(strategy = GenerationType.IDENTITY) 表示自增长方式使用mysql自带的

@Column(name = "id") 表示映射到字段id

注: 其他自增长方式请查看 注解手册

表示name属性映射表的name字段

@Column(name = "price")
public float getPrice() {
return price;
}
表示price属性映射表的price字段
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "product_")
public class Product {
int id;
String name;
float price;

@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") public int getId() { return id; }
public void setId(int id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "price") public float getPrice() { return price; }
public void setPrice(float price) {
this.price = price;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: