Simple integration with Spring-boot and SPRING-DATA-JPA

Source: Internet
Author: User

How to easily use JPA in boot

<!--first introduce JPA dependency--
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-data-jpa</artifactid></dependency>

Take the user role as an example, where the user table, the role table, and an intermediate table, the User Role table, first create the corresponding entity class

/** * User class */  @Data @entitypublic class Account extends Defaultvalidate implements Login {    @Id    @GeneratedValue    private Integer ID;     /**     * Login name */    @NotNull (message = "Login cannot be empty")    @Size (min = 2, max = @Column)    (name= "login_name")    private String loginName;     /**     * Account name */    @NotNull (message = "account name cannot be empty")    @Size (min = 2, max =)    @Column (name= "Account_ Name ")    private String accountname;     /**     * Login Password     *    /@Column (name= "login_pwd")    private String loginpwd;         /**     * Account Identification Code     *    /@Column (name= "Account_code")    private String accountcode;         /**     * Intermediate tables   do not require entity classes to  automatically generate     *    /@ManyToMany (cascade=cascadetype.refresh,fetch=fetchtype.lazy )    @JoinTable (name = "Account_role",    joincolumns = {@JoinColumn (name = "account_id")},     Inversejoincolumns = {@JoinColumn (name = "role_id")})    private set<role> roles = new hashset<role> ();}

  

  

/* Role class * @date March 22, 2018 10:17:11 */@Data @entitypublic class role extends Defaultvalidate {    @Id    @GeneratedVal UE    private Integer ID;     /**     * Role ID */    @NotNull (message = "Role identity cannot be empty")    @Size (min = 2, max =    @Column) (name= "Role_code")    private String Rolecode;     /**     * * Role name */    @NotNull (message = "Role name cannot be empty")    @Size (min = 2, max =    @Column) (name= "Role_name ")    private String roleName;}

The corresponding entity class is created, and the mapping relationship used above is a one-way, many-to-many, followed by the creation of the corresponding repository

@Repositorypublic interface Accountreposity extends Jparepository<account, integer> {     }

The corresponding method can be called when using it. Then to introduce the corresponding dynamic query, dynamic query needs repository inherit Jpaspecificationexecutor class,

Public page<flow> mysubmission (mysubmitpagedto selectdto) {logger.info ("Query my submission start ....") ") ; Pagerequest pageable =new pagerequest (Selectdto.getpagenum (), selectdto.getpagesize ());//anonymous inner class specification< flow> specification = new specification<flow> () {/** * predicate: Represents a query condition * Root: Query entity class * Query: You can get the root object from which to tell Query that entity class, add the query criteria (where the ordering can be set by query) * CB: Used to create criteriabuilder related objects of the factory, from which you can get to predicate object */@Overridepublic predicate Topredicate (root<flow> Root, criteriaquery<?> query, Criteriabuilder CB) {list<predicate> Predicatelist = new arraylist<predicate> ();p Redicatelist.add (Cb.equal (Root.<string>get ("FlowStarter") , Selectdto.getusercode ())); Predicate[] P =new predicate[predicatelist.size ()];return Cb.and (Predicatelist.toarray (P));}}; page<flow> page = Flowrepository.findall (specification, pageable); Logger.info ("Query my submission end ....") "); return page;}

  

If there is a very complex dynamic query, it is recommended to combine SPRING-JDBC to operate, write dynamic native SQL is easier to develop.

Life cycle of JPA objects:

New: Instantaneous object, no ID yet, and object associated with persistence context.

Managed: Persisted managed object, with ID value, has already established an associated object with persistence context.

Datached: A free-form offline object with an ID value, but no associated object with persistence context.

Removed: deleted object with ID value, still associated with persistence context, but ready to remove from database

When the JPA query object is in a persistent state, the database is changed dynamically when the set method of the object property is called. can be done by

@PersistenceContext
Private Entitymanager Entitymanger;

Call the manager's clear method to release the object.

If an enumeration class property is involved, you can override its Set,get method, such as:

Public Warehousestatusenum GetState () {return warehousestatusenum.getwarehousestatusenum (this.state);} public void SetState (Warehousestatusenum state) {this.state = State.value ();}

  

Simple integration with Spring-boot and SPRING-DATA-JPA

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.