hibernate基础配置

1.表名和类名不同,对表名进行配置。

a.Annotation:@Table


package com.wxh.hibernate.model;

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

@Entity
@Table(name="_Teacher")
public class Teacher {
	private int id;
	private String name;
	private String title;
	@Id
	public int getId() {
		return id;
	}
	@Id
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}	
}

2.字段名和属性相同

a.默认为@basic

b.Xml:不用写column


3.字段名和属性名不同

a.Annotation:@column


package com.wxh.hibernate.model;

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

@Entity
@Table(name="_Teacher")
public class Teacher {
	private int id;
	private String name;
	private String title;
	@Id
	public int getId() {
		return id;
	}
	@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;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}	
}


4.不需要psersistence的字段

a.Annotation:@Transient

Xml:不写

package com.wxh.hibernate.model;

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

@Entity
@Table(name="_Teacher")
public class Teacher {
	private int id;
	private String name;
	private String title;
	private String myFatherName;
	
	
	
	@Id
	public int getId() {
		return id;
	}
	@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;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	//这个属性透明,不在数据库存储
	@Transient
	public String getMyFatherName() {
		return myFatherName;
	}
	public void setMyFatherName(String myFatherName) {
		this.myFatherName = myFatherName;
	}	
}


5.映射日期与时间类型,指定时间精度。

a.Annotation:@Temporal

b.Xml:指定type

package com.wxh.hibernate.model;

import java.util.Date; 

import javax.persistence.Column;
import javax.persistence.Entity; 
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="_Teacher")
public class Teacher {
	private int id;
	private String name;
	private String title;
	private String myFatherName;//默认日期和时间都会保存下来
	
	private Date birthDate;
	
	@Id
	public int getId() {
		return id;
	}
	@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;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	//这个属性透明,不在数据库存储
	@Transient
	public String getMyFatherName() {
		return myFatherName;
	}
	public void setMyFatherName(String myFatherName) {
		this.myFatherName = myFatherName;
	}
	@Temporal(TemporalType.DATE)//加上注解后只保留日期
	public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}	
}


6.映射枚举类型

a.@Enumerated

 

7.字段映射的位置(field或者get方法)

a.best practice:保持fieldget set 方法的一致。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值