java 如何修复Sping Boot 中的JPA错误IllegalStateException:列类型未定义?

2nc8po8w  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(107)

我在我的项目中使用JPA + Spring Boot,这是我第一次使用JPA,我对Spring Boot也是个新手。
我想创建两个表file_permsfile_perm_values,我可以正确地定义file_perms,但是,对于我的第二个表file_perm_values,我想定义一个由file_perms的主键组成的复合主键,即:id和另一个字符串file_id。当我编写如下所示的定义并使用Intellij的DDL创建按钮时,得到错误Caused by: java.lang.IllegalStateException: The column type is undefined. Table - file_perm_values; Column - file_perm_id
我哪里做错了?
能不能请一些Maven帮我弄清楚问题是什么以及如何解决这个问题?
我会非常感激的

package com.some.project.persistence.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.time.ZonedDateTime;
import java.util.UUID;

@Entity
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString(onlyExplicitlyIncluded = true)
@Table(name = "file_perms")
public class FilePermsEntity {

    @Id
    @GeneratedValue
    @Builder.Default
    @ToString.Include
    @Column(name = "id", nullable = false)
    private UUID id = null;

    @ToString.Include
    @Column(name = "perm_name", nullable = false)
    private String permName;

    @ToString.Include
    @Column(name = "is_active", nullable = false)
    private boolean active;

    @ToString.Include
    @Column(name = "perm_guid")
    private String permGuid;

    @ToString.Include
    @Column(name = "perm_index")
    private int permIndex;

    @CreatedDate
    @Builder.Default
    @ToString.Include
    @Column(name = "created_at")
    private ZonedDateTime createdAt = ZonedDateTime.now();

    @Builder.Default
    @ToString.Include
    @LastModifiedDate
    @Column(name = "updated_at")
    private ZonedDateTime updatedAt = ZonedDateTime.now();
}
package com.some.project.persistence.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.Table;
import java.io.Serializable;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString(onlyExplicitlyIncluded = true)
@Table(name = "file_perm_values")
public class FilePermValuesEntity {

    @EmbeddedId
    @ToString.Include
    private FilePermValuesPrimaryKey id;

    @ToString.Include
    @Column(name = "value")
    private String value;

    @Getter
    @Builder
    @Embeddable
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString(onlyExplicitlyIncluded = true)
    public static class FilePermValuesPrimaryKey implements Serializable {

        private static final long serialVersionUID = 1223232L;

        @MapsId
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "file_perm_id", nullable = false)
        private FilePermsEntity filePermsEntity;

        @ToString.Include
        @Column(name = "file_id", nullable = false)
        private String fileId;

        @Override
        public boolean equals(Object o) {
            ...
        }

        @Override
        public int hashCode() {
            ...
        }
    }
}
elcex8rz

elcex8rz1#

下面的代码解决了我的问题:

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString(onlyExplicitlyIncluded = true)
@Table(name = "file_perm_values")
public class FilePermValuesEntity {

    @EmbeddedId
    @ToString.Include
    private FilePermValuesPrimaryKey id;

    @MapsId("filePermId")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "file_perm_id")
    private FilePermsEntity filePermsEntity;

    @ToString.Include
    @Column(name = "value")
    private String value;

    @Getter
    @Builder
    @Embeddable
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString(onlyExplicitlyIncluded = true)
    public static class FilePermValuesPrimaryKey implements Serializable {

        private static final long serialVersionUID = 1223232L;

        @ToString.Include
        @Column(name = "file_perm_id", nullable = false)
        private UUID filePermId;

        @ToString.Include
        @Column(name = "file_id", nullable = false)
        private String fileId;

        @Override
        public boolean equals(Object o) {
            ...
        }

        @Override
        public int hashCode() {
            ...
        }
    }
}

相关问题