hibenrate(六)ID的生成策略

ID:
@GeneratedValue注解可以定义该标识符的生成策略:
AUTO - 可以是identity column类型,或者sequence类型或者table类型,取决于不同的底层数据库. (常用)
TABLE - 使用表保存id值 (不重要)
IDENTITY - identity column
SEQUENCE - sequence
例:
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() { ... }

联合主键:
下面是定义组合主键的几种语法:
a)将组件类注解为@Embeddable,并将组件的属性注解为@Id
*StudentPK.java
@Embeddable
public class StudentPK implements java.io.Serializable{
		private int id;
		public int getId() {
			return id;
		}
		…………

*Student.java
Private StudentPK pk;
		@Id
public StudentPK getPK() {
		return PK;
		}

b)将组件的属性注解为@EmbeddedId
*StudentPK.java
public class StudentPK implements java.io.Serializable{
		private int id;
		public int getId() {
			return id;
		}
		…………

*Student.java
Private StudentPK pk;
@EmbeddedId
public StudentPK getPK() {
	return PK;
	}

c)将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id(常用)[/b[b]]*StudentPK.java
public class StudentPK implements java.io.Serializable{
		private int id;
		public int getId() {
			return id;
		}
		…………


*Student.java
@IdClass(StudentPK.class)
public class Student {
private int id;
private String name;
private int age;
	@Id
public int getId() {
		return id;
	}
	@Id
	public String getName() {
		return name;
	}
………………
}

你可能感兴趣的:(java,C++,c,C#)