Kihagyás

3. gyakorlat

Perzisztencia...

  • előfeltételek: MySQL vagy Postgre
  • JPA https://spring.io/projects/spring-data-jpa (specifikáció) --> Hibernate (implementáció)
  • deps:
    • Tipp: Úgy dependency-k hozzáadásához használjuk a pom.xml-en belül az Alt + Insert billentyűkombinációt.

      1
      2
      3
      4
      5
      6
      7
      8
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-data-jpa</artifactId>
          </dependency>
          <dependency>
              <groupId>com.h2database</groupId>
              <artifactId>h2</artifactId>
          </dependency>
      
  • application.properties https://www.baeldung.com/spring-boot-h2-database#database-configuration
    1
    2
    3
    4
    5
    spring.datasource.url=jdbc:h2:mem:testdb
    spring.datasource.driverClassName=org.h2.Driver
    spring.datasource.username=sa
    spring.datasource.password=sa
    spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
    
  • Hikari Pooling

  • http://localhost:8080/h2-console
    • JDBC URL az app.props-ból
    • Tipp: IntelliJ > Database fül > Conn-type: In-memory

  • Contact model:
    • @Entity
    • @Id
  • csekkolni a h2-console-on
  • @Table(name = "CONTACT_TABLE")
  • @Column(name = "name_col", nullable = false) vs. @NotNull
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    package hu.suaf.contacts.model;
    
    import lombok.Data;
    
    import javax.persistence.*;
    import javax.validation.constraints.*;
    import java.util.Date;
    
    @Data
    @Entity
    @Table(name = "CONTACT_TABLE")
    public class Contact {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @NotEmpty(message = "Name cannot be empty")
        @Size(min = 3, message = "Name must be at least 3 characters long")
        @Column(name = "name_col", nullable = false)
        private String name;
    
        @NotNull
        private String phone;
    
        @NotEmpty(message = "Email cannot be empty")
        @Email(message = "Must be a well-formed email")
        private String email;
        private String address;
    
        @NotNull(message = "Birth date cannot be empty")
        @Past(message = "Must be a past date")
        private Date birthDate;
    
        @PastOrPresent
        private Date createdAt;
    
        @ManyToOne
        @JoinColumn(name = "group_id")
        private ContactGroup contactGroup;
    
    }
    
  • új entity:
    • Group --> neve miatt elszáll
      • id, name
    • ContactGroup vagy @Table
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      package hu.suaf.contacts.model;
      
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.OneToMany;
      import java.util.List;
      
      @Entity
      public class ContactGroup {
      
          @Id
          private long id;
      
          private String name;
      
          @OneToMany
          private List<Contact> contacts;
      
      }
      
  • Contact-ban kapcsolatok
    • @OneToOne
    • @OneToMany
    • @ManyToOne
    • @ManyToMany
    • extrák:
      • @JoinColumn
      • @JoinTable
    • Group-ban a visszafelé irány (demo, majd kikommentez)
      • List contacts
    • class ContactRepository
      • interface ContactRepository extends JpaRepository<Contact, Long>
      • < típus, id >
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        package hu.suaf.contacts.repository;
        
        import hu.suaf.contacts.model.Contact;
        import org.springframework.data.jpa.repository.JpaRepository;
        import org.springframework.stereotype.Repository;
        
        @Repository
        public interface ContactRepository extends JpaRepository<Contact, Long> {
        
        }
        
    • a ContactService-ben helyettesítsük be az új metódusokat
      • az addContact és a saveContact összevonása
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        package hu.suaf.contacts.service;
        
        import hu.suaf.contacts.model.Contact;
        import hu.suaf.contacts.repository.ContactRepository;
        import lombok.extern.slf4j.Slf4j;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        
        import java.util.List;
        
        @Service
        @Slf4j
        public class ContactService {
        
            private ContactRepository contactRepository;
        
            @Autowired
            public void setContactRepository(ContactRepository contactRepository) {
                this.contactRepository = contactRepository;
            }
        
            public void saveContact(Contact c){
                contactRepository.save(c);
                log.info("Contact added: " + c.toString());
            }
        
            public List<Contact> getContacts(){
                return contactRepository.findAll();
            }
        
            public void deleteContact(long id){
                contactRepository.deleteById(id);
                log.info("Contact deleted with id: " + Long.toString(id));
            }
        
            public Contact getContactById(long id) {
                return contactRepository.findById(id).orElse(null);
            }
        
        }
        
    • auto adat beszúrás
      • resources-ban: data.sql vagy schema.sql

Profilok...

  • resources-ban:
    • application-dev.properties:
      1
      2
      3
      4
      5
      spring.datasource.url=jdbc:h2:mem:testdb
      spring.datasource.driverClassName=org.h2.Driver
      spring.datasource.username=sa
      spring.datasource.password=sa
      spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
      
    • application-prod.properties:
    • spring.profiles.active=prod
      • Tipp: IntelliJ > run config > active profiles

    • pom.xml:
      1
      2
      3
      4
      <dependency>
          <groupId>org.postgresql</groupId>
          <artifactId>postgresql</artifactId>
      </dependency>
      

Feladat

Egészítsük ki a Contact Create form-ot a group megadásának lehetőségével, valamint készétsük el a ContactGroup CRUD műveleteihez szükséges felületeket/controller-eket/stb.

További anyagok

A gyakorlat anyagáról készült videó:

SUAF_03_gyak


Utolsó frissítés: 2021-09-29 22:22:18
Back to top