1, a statement for persistent fields accessors (accessors) and whether the variable signs (mutators)
Property statement is not necessarily required for the public's. Hibernate can be default, protected or private of the get / set methods on the implementation of the property equally persistent.
2, the realization of a default constructor (constructor)
All persistent classes must have a default constructor (may not be public), and so Hibernate can use Constructor.newInstance () to instantiate them.
3, to provide a property ID (identifier property) (optional)
This property can be called any name, its type can be any of the original type, original type of packaging type, java.lang.String or java.util.Date. (If your old database table has composite keys, you can even use a user-defined categories, one of each property are one of these types. However, the requirement is optional, some of the features only a logo statement the role of property type.
Added:
Agent (proxies), require persistent class is not final, or is a full public interface methods are the specific implementation. You can for a final, but did not achieve the implementation of a durable type of interface, but the agent should not be used against them - more or less will affect your choice for performance optimization. session of the load is the agent used. Lazy loading a delivery back into the category of sub-class entity.
Persistence Life Cycle (Lifecycle) in the callback (Callbacks)
As an optional step, be persistent class can achieve Lifecycle interface, it can provide some for the callback method, allowing persistent object in the save or load following, or delete or update the necessary initialization before with clear steps.
public interface Lifecycle {
public boolean onSave(Session s) throws CallbackException;
public boolean onUpdate(Session s) throws CallbackException;
public boolean onDelete(Session s) throws CallbackException;
public void onLoad(Session s, Serializable id);
} onSave - at the target will soon be time to save or insert callback
onUpdate - at the object about to be update when the callback (ie, object is passed to the Session.update () time)
onDelete - at the object about to be delete (delete) when the callback
onLoad - has just been at the target load (loading) after the time of callback
onSave (), onDelete () and onUpdate () can be used to cascade save or delete the object of dependence. This practice is stated in the mapping file to operate outside the cascade another selection. onLoad () can be used to allow the object from its persistent (current) state to initialize some temporary property. Should not use this approach to load dependent objects, because this method may not be able to call the internal Session interface. onLoad (), onSave () and onUpdate () are used for another use at the current Session save a reference has been prepared for his own benefit.
Please note onUpdate () is not at each persistent object state is updated when it was called. It has not been at in a persistent object is passed to the Session.update () time will be called.
If onSave (), onUpdate () or onDelete () return true, then the operation has been quietly canceled. If one of the dished out CallbackException abnormal operation was canceled, this anomaly will be continue to be passed to the application.
Please note onSave () are in the object identifier has been given after the call, unless the use of local (native) way to generate keywords.
<hibernate-mapping package="hibernatetest">
<class name="User" table="user">
<id name="id">
<generator></generator> //使用主键生成器为hilo时,打印输出1.如果改为native,则打印输出为0.
</id>
<property name="userName"></property>
<property name="birthday"></property>
</class>
</hibernate-mapping> public class User implements Lifecycle {
public boolean onDelete(Session arg0) throws CallbackException {
throw new UnsupportedOperationException("Not supported yet.");
}
public void onLoad(Session arg0, Serializable arg1) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean onSave(Session arg0) {
System.out.println("onSave!");
System.out.println(this.id);
return true; //这里返回ture,导致保存操作被取消,检查数据库表,果然未插入成功。改为返回false,则插入记录成功。
}
public boolean onUpdate(Session arg0) throws CallbackException {
throw new UnsupportedOperationException("Not supported yet.");
}
private int id;
private String userName;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
} In the main function will be to preserve user object callback onSave () method.







