java jakarta.persistence.Check约束不起作用

tkclm6bt  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(114)

我在Sping Boot 项目中有以下Movie类,它包含演员列表。我想确保演员不会被删除,如果它是电影的一部分,我想通过JPA/Hibernate在数据库级别上强制执行此约束。然而,当我导入jakarta.persistence.Check时,编译器无法识别它,即使我在gradle中有正确的依赖关系。我假设这是版本控制问题,但我不能。I don’我无法解决这个问题。

import jakarta.persistence.*;
    import jakarta.persistence.Check;
    
    
    import lombok.AllArgsConstructor;
    import lombok.Getter;
    import lombok.NoArgsConstructor;
    import lombok.Setter;
    
    import java.time.LocalDate;
    import java.util.HashSet;
    import java.util.Set;
    
    @Entity
    @Table(name = "movies",
            uniqueConstraints = @UniqueConstraint(columnNames = {"title", "releaseDate"}))
public class Movie extends Media {

    @Column(nullable = false)
    private String title;

    @Column(nullable = false)
    private LocalDate releaseDate;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "movie_actor",
            joinColumns = @JoinColumn(name = "movie_id"),
            inverseJoinColumns = @JoinColumn(name = "actor_id"),
            uniqueConstraints = {@UniqueConstraint(columnNames = {"movie_id", "actor_id"})},
            foreignKey = @ForeignKey(name = "fk_movie"),
            inverseForeignKey = @ForeignKey(name = "fk_actor"),
            // add check constraint to ensure actor is not deleted if part of any movie
            constraints = @Check(constraints = "NOT EXISTS(SELECT * FROM movie_actor ma WHERE ma.actor_id = id)")
    )
    private Set<Actor> actors = new HashSet<>();
}

以下是我的gradle.build文件:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.5'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'io.freefair.lombok' version '8.0.1'
}

group = 'com.bill'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    //jcenter()
}

dependencies {
    implementation 'org.springframework.data:spring-data-jpa:3.0.4'
    implementation 'javax.persistence:javax.persistence-api:2.2'
    implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0'
    implementation 'jakarta.validation:jakarta.validation-api:3.0.2'
    implementation 'org.springframework.boot:spring-boot-starter-validation:2.6.3'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'mysql:mysql-connector-java:8.0.32'
    implementation 'org.hibernate:hibernate-core:5.5.7.Final'
    implementation 'org.hibernate:hibernate-entitymanager:5.5.7.Final'

/*

 */

    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

tasks.withType(JavaCompile) {
    options.annotationProcessorPath = configurations.compileClasspath
}
h5qlskok

h5qlskok1#

Jakarta Persistence API从来没有定义过@Check注解。所以,jakarta.persistence.Check在2.3和3.x规范中都不存在。最新的规范是here,源代码是here
我假设您想使用org.hibernate.annotations.Check有关详细信息,请访问此链接:https://docs.jboss.org/hibernate/orm/5.5/userguide/html_single/Hibernate_User_Guide.html#schema-generation-database-checks。
最后,我想指出这段代码的一些重要部分。

  1. Hibernate Entitymanager 5.5.7已到达生命周期结束阶段。不再支持此版本。我强烈建议升级此依赖项。
  2. spring-data-jpa:3.0.4有一个可选的依赖项hibernate-core:6.1.4.Final。这是因为我推荐使用Hibernate 6.x。
    1.您在依赖项中定义了两个不兼容的JPA API:javax.persistence:javax.persistence-api:2.2jakarta.persistence:jakarta.persistence-api:3.1.0 Spring Framework 6和Sping Boot 3都完全兼容Jakarta EE 9。在Jakarta堆栈上使用任何javax.*依赖项都可能导致问题。

相关问题