Get the App
SLTechnology News&Howtos  ›  Development  › 

How to extract ID as the base class when using Hibernate as the model layer

Shulou Source: shulou.com Published: 2022-06-02 08:00:05 10月03日 Update

This article mainly explains "how to use Hibernate to do the model layer to extract ID as the base class", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use Hibernate to do model layer to extract ID as the base class" bar!

When making a model, we always define a lot of Javabean, which can be used to replace data. When we use Hibernate as the persistence layer, we always set the field id as the primary key when building the table. It has nothing to do with the project, so it is safer for us to update the data. Now let's extract it as the base class of the model layer, where we can override the equals (), hashCode (), toString () methods. The code is as follows:

Java code

Packagecom.hyq.blog.model; import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ReflectionToStringBuilder; public classBaseModel implements Serializable {private String id; public String getId () {return id;} public void setId (String id) {this.id=id } / / override the equals () method public boolean equals (Object obj) {if (this==obj) return true; if (! (obj instance BaseModel)) return false; BaseModel target= (BaseModel) obj; if (this.getId ()! = null&&this.getId (). Length () > 0) {return this.getId () .equals (target.getId ()) } if (target.getId ()! = null&&target.getId (). Length () > 0) {return false;} return EqualsBuilder.reflectionEquals (this,obj);} / / override the hashCode () method public int hashCode () {if (this.getId ()! = null&&this.getId (). Lenght () > 0) {return this.getId (). HashCode ();} return HashCodeBuilder.reflectionHashCode (this) } / / override the toString () method public String toString () {return ReflectionToStringBuilder.toString (this) }} Thank you for your reading, the above is the content of "how to use Hibernate as the model layer to extract ID as the base class". After the study of this article, I believe you have a deeper understanding of how to use Hibernate as the model layer to extract ID as the base class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Model extraction method learning code content data security nothing fields that is ideas situations articles more knowledge knowledge points articles follow questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS vpn Redmi Linux