Annotations:
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id;XML:
<id name="id"> <generated-value strategy="AUTO"/> </id>
Annotations:
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)XML:
<id name="id"> <generated-value strategy="IDENTITY"/> </id>
Annotations:
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGen") @SequenceGenerator(name = "sequenceGen", sequenceName = "Id_Gen_Sequence", initialValue = 100, allocationSize = 30)XML:
<id name="id"> <generated-value strategy="SEQUENCE" generator="sequenceGen"/> <sequence-generator name="sequenceGen" sequence-name="Id_Gen_Sequence" initial-value="100" allocation-size="30"/> </id>
Annotations:
@Id @GeneratedValue(strategy=GenerationType.TABLE, generator="tableGen") @TableGenerator(name="tableGen", table="Id_Gen_Table", pkColumnName="entityType", valueColumnName="maxId", pkColumnValue="Employee", initialValue=1, allocationSize=1)XML:
<id name="id"> <generated-value strategy="TABLE" generator="tableGen"/> <table-generator name="tableGen" table="Id_Gen_Table" pk-column-name="entityType" value-column-name="maxId" pk-column-value="Employee" initial-value="1" allocation-size="1" /> </id>
@Entity public class Employer { @Id private Long id; } @Embeddable public class DepartmentId { private String name; private Long employer; // getter/setter, equals(), hashCode() } @Entity public class Department { @EmbeddedId private DepartmentId id; @MapsId("employer") @ManyToOne private Employer employer; public Department() { } public Department(String name, Employer employer) { this.id = new DepartmentId(); this.id.setName(name); this.id.setEmployer(employer.getId()); this.employer = employer; } }The Department entity identifier is DepartmentId(EmbeddedId). The "employer" relation attribute of Department is mapped to the "employer" field of the DepartmentId. So there will not be a separated database column for the relation attribute "employer". Note that the type of "employer" field is the identifier type of parent entity "Employer".
When setting the identifier of a dependent entity, also set corresponding MapsId relation attribute (if any) value to its parent entity as the example constructor Department(String name, Employer employer) above. They must be consistent.
The following code is IdClass version.
@Entity public class Employer { @Id private Long id; } public class DepartmentId { private String name; private Long employer; // getter/setter, equals(), hashCode() } @Entity @IdClass(DepartmentId.class) public class Department { @Id private String name; @Id @ManyToOne private Employer employer; }