Spring Data JPA Common annotations @Query, @NamedQuery

Source: Internet
Author: User

1. @Transient

@Transient indicates that the property is not a mapping to a field in a database table, and the ORM framework ignores the property;
If a property is not a field mapping for a database table, it must be marked as @transient, otherwise the ORM framework defaults to its annotation as @basic;

Indicates that the field is not in the database table

@Transient
public int getage () {
return;
}

Jackson Related:

2. @JsonIgnoreProperties

This annotation is a class annotation that, when JSON is serialized, ignores some properties in the Java Bean, and both serialization and deserialization are affected.

3. @JsonIgnore

This annotation is used on properties or methods (preferably on attributes), as in the @jsonignoreproperties above.

4. @JsonFormat

This annotation is used for attributes or methods (preferably on attributes), and it is convenient to convert the date type directly into the pattern we want, such as @jsonformat (pattern = "Yyyy-mm-dd hh-mm-ss")

5. @JsonSerialize

This annotation is used on attributes or Getter methods to embed our custom code when serializing, such as when serializing a double with a two-bit decimal point behind it.

6. @JsonDeserialize

This annotation is used on attributes or setter methods, and can be used to embed our custom code when deserializing, similar to the above @jsonserialize

* The JPA namedqueries can be used as follows:1: Use @namedquery on the entity class, as shown in the following example:@NamedQuery (name = "Usermodel.findbyage", query = "Select O from Usermodel o where o.age >=? 1")
Note: Use the following annotations when defining multiple
@NamedQueries (value = {
@NamedQuery (name = User.query_find_by_login,
query = "Select u from User u where u." + User.prop_login
+ "=: username"),
@NamedQuery (name = "Getusernamepasswordtoken",
query = "Select New Com.aceona.weibo.vo.TokenBO (U.username,u.password) from User u where u." + User.prop_login
+ "=: Username")}) 2: Define a method of the same name in the Repository interface of the DAO that you implement, with the following example:Public list<usermodel> Findbyage (int.);3: Then it can be used, and spring will first find out if there is a namedquery with the same name, and if so, it will not be parsed according to the method defined by the interface. * Using @querysuch a query can be declared in an inherited Jparepository interface method that can be used on a custom query method to specify the query statement that the method executes, such as:@Query ("Select O from Usermodel o where o.uuid=?1")Public list<usermodel> findbyuuidorage (int uuid); Note:1: The number of parameters of the method must be the same as the number of parameters required in @query2: If it is like, the following parameters need to be preceded or followed by "%", for example:@Query ("Select O from Usermodel o where o.name like? 1%")Public list<usermodel> findbyuuidorage (String name); @Query ("Select O from Usermodel o where o.name like%?1")Public list<usermodel> findbyuuidorage (String name); @Query ("Select O from Usermodel o where o.name like%?1%")Public list<usermodel> findbyuuidorage (String name); of course, it is possible to pass the parameter value when you can not add '% ', of course, it is not wrong to add n You can also use @query to specify local queries, as long as you set Nativequery to True, such as:@Query (value= "select * from Tbl_user where name is like%?1", Nativequery=true)Public list<usermodel> findbyuuidorage (String name); Note: The current version of the local query does not support page flipping and dynamic sorting Using named parameters, you can use @param, for example:@Query (value= "Select O from Usermodel o where o.name like%:nn")Public list<usermodel> Findbyuuidorage (@Param ("nn") String name);also support for updating the query statement of the class, add @modifying can, for example:@Modifying@Query (value= "Update Usermodel o set o.name=:newname where o.name like%:nn")public int Findbyuuidorage (@Param ("nn") string name, @Param ("NewName") string newName); Note:1: The return value of the method should be int, indicating the number of rows affected by the UPDATE statement2: A transaction must be added at the place of invocation, no transaction can be performed normally

    

Spring Data JPA Common annotations @Query, @NamedQuery

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.