spring JPA practice record

Develop a small feature

mysql database associated POJO



 1 package *;
 2 
 3 import lombok.Data;
 4 import lombok.NoArgsConstructor;
 5 
 6 import javax.persistence.Column;
 7 import javax.persistence.Entity;
 8 import javax.persistence.Id;
 9 import javax.persistence.Table;
10 
11 @Data
12 @Entity
13 @NoArgsConstructor
14 @Table(name = "alarm_topics")
15 public class AlarmTopicsEntity {
16 
17     @Column(name = "name", length = 255, nullable = false)
18     private String name;
19 
20     @Id
21     @Column(name = "topic_urn", unique = true, nullable = false)
22     private String topicUrn;
23 
24 
25     @Column(name = "creator", length = 255)
26     private String creator;
27 
28     @Column(name = "created_time")
29     private long createdTime;
30 
31     @Column(name = "updated_time")
32     private long updatedTime;
33 
34     @Column(name = "rules", length = 4096)
35     private String rules;
36 }


among them
topicUrn index for the ID


 1 package *.dao.api;
 2 
 3 import *.model.AlarmTopicsEntity;
 4 import org.springframework.data.jpa.repository.JpaRepository;
 5 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 6 
 7 import java.util.List;
 8 
 9 public interface AlarmTopicDao extends JpaRepository<AlarmTopicsEntity, String>, JpaSpecificationExecutor<AlarmTopicsEntity> {
10 
11 
12 //    /**
13 //     * findAll
14 //     *
15 //     * @param spec     spec
16 //     * @param pageable pageable
17 //     * @return Page<TopicsEntity>
18 //     */
19 //    Page<AlarmTopicsEntity> findAll(Specification<AlarmTopicsEntity> spec, Pageable pageable);
20 //
21 //    List<AlarmTopicsEntity> findAll(Specification<AlarmTopicsEntity> spec);
22 
23 
24   //   List<AlarmTopicsEntity> findByRulesLike(String rule);
25 
26 //    List<AlarmTopicsEntity> findByTopicUrn(String topicUrn);
27 
28 //    void deleteByTopicUrn(String topicUrn);
29 
30 
31 //    @Query("select topicUrn from alarm_topics where rules like %:rule%")
32 //    List<String> getTopicUrnByRule(@Param("rule") String rule);
33 //
34 //
35 //
36 //    @Transactional
37 //    @Modifying
38 //    @Query("delete from AlarmTopicsEntity where topic_urn = ?1")
39 //    void deleteByTopicUrn(@Param("topic_urn") String topicUrn);
40 //
41 //
42 //    @Query("select AlarmTopicsEntity from AlarmTopicsEntity where topic_urn = ?1")
43 //    AlarmTopicsEntity getByTopicUrn(@Param("topic_urn") String topicUrn);
44 //
45 
46 
47 
48 }

 



transfer
. 1  public  void deleteTopic (String topicUrn) {
 2  
. 3      alarmTopicDao.deleteById (topicUrn);
 . 4  // noted herein is not deleteByTopicUrn, AlarmTopicDao do not have to be declared 
. 5  }
 . 6  
. 7  public AlarmTopicsEntity getTopicByTopicUrn (String topicUrn) {
 . 8  
. 9      return alarmTopicDao. the findById (topicUrn) .orElse ( null );
 10  
. 11 }

 

findById (topicUrn) rather than findByTopicUrn,
 alarmTopicDao.findById (topicUrn) Optional return a result, you can usually () method for determining whether a isPresent results, may be used

 

 

 

 jpa support the following naming conventions apply to

 

 

 

 

The relationship between JPA and Hibernate

JPA (Java Persistence API), is a standard Java EE ORM hub 5, is ejb3 part of the specification.

Hibernate is a very popular before the ORM framework , is an implementation of JPA , there are other frameworks Toplink ROM and the like.

 

Hibernate is mainly achieved through three components:

  • hibernate-core: Hibernate's core implementation , Hibernate provides all the core functionality.
  • EntityManager-Hibernate: Hibernate JPA implements standard, it can be seen between the adapter and JPA hibernate-core, it does not directly provide the ORM function, but rather hibernate-core encapsulation, such Hibernate JPA-compliant .
  • annotation-Hibernate: Hibernate support base configuration of annotation mode , which includes the standard JPA annotation and the annotation Hibernate its own special features.
@id defines the primary key attribute is mapped to a database table, a solid can have a primary key attribute is mapped.

@GeneratedValue (strategy = GenerationType, generator = "")
Optional
strategy: a front key generation strategy, there AUTO, INDENTITY, SEQUENCE, and TABLE 4 species respectively selected so ORM framework automatically generates a database field in accordance with Identity, according to the database table the Sequence field is generated, there is an additional table to generate a primary key, the default is AUTO.

generator: indicates the name of the primary key generator, and this property is typically associated ORM framework, e.g., Hibernate uuid the like can specify the primary key generated.




@Column
Alternatively
@Column described in detail, the definition of the field in the database table, which generation means for JPA annotations database table structure according very useful.
name: Name of the database table name of the field, the default property name the same situation.
nullable: indicates that the field is allowed to be null, the default is true.
unique: Indicates whether the field is a unique identifier, the default is false.
length: indicates the size of the field, the field is valid only for type String.

 

Guess you like

Origin www.cnblogs.com/mushimeng/p/11562986.html