Spring JPA Entity attribute type converter Attributeconverter

Source: Internet
Author: User

1, Introduction interface attributeconverter<x, y>

This interface is used to convert entity properties, but the id attribute and the Relationship property are not available. It has two methods:

1, y Converttodatabasecolumn (x) Action: The entity attribute x is converted to y stored in the database, that is, the insert and update operations are performed;

2, x Converttoentityattribute (Y) Action: Convert the field y in the database to entity attribute x, which is executed when the query operation

2, the implementation of the demo

Requirements: A status column in a database table, 1 for Enable, 1 for disabled, and 2 for deleted.

Step one: Create Statusenum, listing only some of the key codes

Public enum Statusenum {enable (1, "Enabled"), DISABLE (-1, "disabled"), DELETED (-2, "deleted");        Statusenum (Integer value, String description) {this.value = value;    this.description = description; }}

Step two: Implement Attributeconverter<string, Integer> interface, will represent the number and description of the conversion, where the Status field in the Entity class is a string type

public class statusattributeconverter implements attributeconverter<string,  integer> {     @Override     public Integer  Converttodatabasecolumn (String status)  {        try {             return integer.parseint (status);     //if it is a number, it is returned directly (this can be further verified by traversing the value of Statusenum)          } catch  (numberformatexception e)  {             for  (Statusenum type : statusenum.values ())  {     //if it is not a number, use Statusenum to find a description of the corresponding number                  if  (Status.equals (type.getdescription))  {           &nBsp;         return type.getvalue ();                 }             }        }         throw new runtimeexception ("unknown statusenum: "  +  status);     //If there is no representation number or description in the Statusenum, an exception is thrown     }      @Override     public string converttoentityattribute (Integer value)  {        for  (statusenum type :  Statusenum.values ())  {    //converts a number to a description              if  (Value.equals (type.getvalue))  {             &nbSp;   return type.getdescription ();             }        }         throw new runtimeexception ("unknown database value: "  + value);     }}

Step three: Look at the entity class

@Entity @table (name = "T_demo") public class Demoentity {@Convert (converter = statusattributeconverter.class) Private String status; Status: 1 enabled, 1 disabled,-2 deleted}
3. Advantages

The client does not have to hardcode the background numbers with the corresponding details of the description.

The server provides an interface to get Statusenum, gets to [{"Value": 1, "description": "Enabled"}, {"Value":-1, "description": " Disabled "}, {" Value ": -2,"description":" deleted "}], the client is assembled directly into a drop-down list.



Copyright = {"Author": "Ink Night", "article link": "http://my.oschina.net/letao/blog/524487"}


Spring JPA Entity attribute type converter Attributeconverter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.