Saya sedang melakukan pemeliharaan kode ini, bisakah seseorang menjelaskan apa itu notasi Post_ pada kode di bawah ini? Saya tahu Post adalah model yang saya buat, tetapi Post_ dengan garis bawah tidak ada dalam file. Saya mencari tetapi tidak menemukan informasi tentang ini. Ini adalah file Boot Musim Semi yang dibuat sebelumnya? Dan jika demikian bagaimana cara membuatnya?

import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;

import org.springframework.data.jpa.domain.Specification;

import com.example.demo.dto.filter.PostFilter;
import com.example.demo.model.Post;
import com.example.demo.model.Post_;

public class PostSpecs extends BaseSpecs {

    public static Specification<Post> specByFilter(Optional<PostFilter> filter) {
        return filter.isEmpty() ? null : (root, query, builder) -> {
            Collection<Predicate> predicates = new ArrayList<>();

            predicates.add(equal(builder, root.get(Post_.ID), filter.map(PostFilter::getId)));
            predicates.add(contains(builder, root.get(Post_.TITLE), filter.map(PostFilter::getTitle)));
            predicates.add(contains(builder, root.get(Post_.BODY), filter.map(PostFilter::getBody)));

            Expression<String> allCols = concatAll(builder, root.get(Post_.ID), root.get(Post_.TITLE),
                    root.get(Post_.BODY));
            predicates.add(contains(builder, allCols, filter.map(PostFilter::getAny)));
            return toAndArray(builder, predicates);
        };
    }
}

Posting Kelas

package com.example.demo.model;

import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @CreatedDate
    private LocalDateTime createdAt;
    @LastModifiedDate
    private LocalDateTime updatedAt;
    @CreatedBy
    private Long createdBy;
    @NotBlank
    @Column(length = 100)
    private String title;
    @NotBlank
    @Column(length = 500)
    private String body;
    @ManyToOne
    private User user;

    public Post() {
    }

    public Post(Long id, String title, String body) {
        this.id = id;
        this.title = title;
        this.body = body;
    }

    public Post(String title, String body) {
        this.title = title;
        this.body = body;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Long getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

Pom.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <springdoc.version>1.4.3</springdoc.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>${springdoc.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-data-rest</artifactId>
            <version>${springdoc.version}</version>
        </dependency>
        
        <!-- Lorem Ipsum to generate test data -->
        <dependency>
            <groupId>com.thedeanda</groupId>
            <artifactId>lorem</artifactId>
            <version>2.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>3.3.3</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <processors>
                                <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                            </processors>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>${hibernate.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>
1
Gerson 30 September 2021, 15:16

2 jawaban

Jawaban Terbaik

Itulah hasil dari penggunaan Hibernate JPA 2 Metamodel Generator (lihat hibernate-jpamodelgen di pom.xml Anda.

Anda dapat menemukan informasi lebih lanjut tentangnya di https://docs .jboss.org/hibernate/stable/jpamodelgen/reference/en-US/html_single/.

Hibernate Static Metamodel Generator adalah prosesor anotasi berdasarkan [Pluggable Annotation Processing API] dengan tugas membuat kelas metamodel statis JPA 2.

0
João Dias 30 September 2021, 14:16

Tujuan utama Post_ adalah jenis kueri JPA yang aman.

Sebenarnya Post_ bukanlah hal yang besar. Ini adalah konvensi yang digunakan oleh JPA sendiri. Ini adalah kelas metamodel dari kelas model ketekunan JPA. Post_ adalah metamodel dari kelas Post. Ini memberikan nama bidang kelas Post Anda dan jenisnya untuk JPA

0
naimul 30 September 2021, 14:54