簡體   English   中英

Spring Boot Data JPA manyToMany添加項引發UnsupportedOperationException

[英]Spring Boot Data JPA manyToMany add item throws UnsupportedOperationException

Company.java:

@Entity
@JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id")
@Data
public class Company {
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    String id;

    String name;

    @ManyToMany
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<User> employees = new ArrayList<>();
    // Snip
}

用戶

@Entity
@JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id")
@Data
public class User {
    @Id @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    String id;

    @NotNull
    String email;

    @NotNull
    Date created = new Date();

    @ManyToMany(mappedBy="employees")
    @LazyCollection(LazyCollectionOption.FALSE)
    List<Company> employedByCompanies = new ArrayList<>();
    // Snip
}

庫:

public interface CompanyRepository extends CrudRepository<Company, String> {
}

public interface UserRepository extends CrudRepository<User, String> {
}

因此,我從數據庫加載了一家公司,然后執行以下操作:

company.getEmployees().add(timesheetUser);

我得到例外:

java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)

為什么JPA沒有使用我可以更新的類?

這是我的gradle文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'timesheets-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("io.jsonwebtoken:jjwt-api:0.10.5")
    compile("io.jsonwebtoken:jjwt-impl:0.10.5")

    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.cloud:spring-cloud-starter-config")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile('org.springframework.boot:spring-boot-starter-actuator')

    compile group: 'org.springframework.security.oauth.boot', name: 'spring-security-oauth2-autoconfigure', version: '2.0.3.RELEASE'

    compile("com.h2database:h2")
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
    compile group: 'com.google.guava', name: 'guava', version: '26.0-jre'
    compileOnly 'org.projectlombok:lombok:1.18.2'
    runtime 'io.jsonwebtoken:jjwt-impl:0.10.5',
            // Uncomment the next line if you want to use RSASSA-PSS (PS256, PS384, PS512) algorithms:
            //'org.bouncycastle:bcprov-jdk15on:1.60',
            'io.jsonwebtoken:jjwt-jackson:0.10.5'

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.BUILD-SNAPSHOT"
    }
}

repositories {
    maven {
        url 'https://repo.spring.io/libs-milestone'
    }
    maven {
        url 'https://repo.spring.io/libs-snapshot'
    }
}

無論出於什么原因,我都不得不將List更改為Set並且它可以工作。

像這樣:

@Entity
@JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id")
@Data
public class User {
    @Id @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    String id;

    @NotNull
    String email;

    @NotNull
    Date created = new Date();

    @ManyToMany(mappedBy="employees")
    @LazyCollection(LazyCollectionOption.FALSE)
    // List DOES NOT WORK HERE!
    Set<Company> employedByCompanies = new HashSet<>();
    // Snip
}

不知道為什么

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM