Hibernate Id Generator and Primary Key

Use automate id by hibernate:

If you want the tables' id be created automation. How to do it?

When use XML file, Just use the generator:

 <id name="id"> 

   <generator class="native"></generator>

  </id> 

Now id which is named id will be created automation. The class native can automatically identify Database you use.

You can also use uuid or hilo to get a single number. uuid can generate a number unique all the universe.

When use annotation, Just use the @GeneratedValue under @Id if you want to automate id.

Use @GeneratedValue by default. All database can automate id.

When the Database which suport identity. @GeneratedValue(strategy = GenerationType.IDENTITY).

When use database which suport sequence. @GeneratedValue(strategy = GenerationType.SEQUENCE)

The best way to let your data and program can be used anywhere is use like this:

 @TableGenerator

@TableGenerator(
ame="DOCTOR_GEN",
table="GENERATOR_TABLE",
pkColumnName = "pk_key",
valueColumnName = "hi",
pkColumnValue="doctor",
allocationSize=1 ) /*Use TableGenerator * Create a table which is named GENERATOR_TABLE and includes two column * Column "Key" and column "hi" * set a value in column Key which is named teacher * then the table which is named teacher create id will get it form GENERATOR_TABLE * at the last the value of column hi do add by allocationSize * */

TableGennerator create a table to save number for other tables;

Use  @GeneratedValue(strategy=GenerationType.TABLE, generator="DOCTOR_GEN") to use Generator table.

Sometimes we need more than one column to be primary key.

So we Create a class to provide object which object package primary key.

 public class ProgramerPK implements Serializable{
private int id;
private String sid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
} @Override
public boolean equals(Object o){
if(o instanceof ProgramerPK){
ProgramerPK sp = (ProgramerPK)o;
if(this.id == sp.getId() && this.sid == sp.getSid()){
return true;
}
}
return false;
} @Override
public int hashCode(){
return this.sid.hashCode();
}
}

This class has two elements named id and sid. But we must implement the Serializable interface.

The Serializable interface can make this class be serialized.

Then we must also to override the equals method and hashCode method.

The equals which is overrode can assert objects real equal or not.

The hashCode which is overrode can get real hashCode for object.

And the hashCode will be used for serializeble.

If use XML file to configure hibernate do like this:

<composite-id class="StudentPK" name="stp">
<key-property name="id"></key-property>
<key-property name="sid"></key-property>
</composite-id>

If use annotation just write like this:

 @EmbeddedId
//use many of columns be ID
public ProgramerPK getPp() {
return pp;
}

The source code : https://github.com/andy201401/hibernate_learn/tree/master/hibernat_0400_ID

上一篇:Database Primary key and Foreign key [From Internet]


下一篇:如何通过js和jquery获取图片真实的宽度和高度