Hibernate很多涉及一个3表

时间:2014-05-04 18:36:33

标签: java mysql hibernate many-to-one

我从hibernate开始,我在构建简单方面遇到了一些困难 表的关系

,hibernate throw嵌套异常是org.hibernate.MappingException:无法找到具有逻辑名的列:org.hibernate.mapping.Table(USER_FILESYSTEM)中的USERNAME及其相关的supertables和secondary tables

我有这个模型

USER - 1 --- N - > USER_FILE_SYSTEM - 1 - N - > USER_FS_THUMBS

DB squema上的所有列名称都是OK,列用户名“存在于表USER_FILESYSTEM上 和表用户

表用户

    @Entity
@Table(name = "USERS")
public class Users implements Serializable {

    @Id
    @Column(name = "USERNAME", nullable = false, length = 15)
    private String username;

    @Column(name = "PASSWORD", nullable = false, length = 64)
    private String password;

    @Column(name = "FIRSTNAME", length = 32)
    private String firstname;

    @Column(name = "LASTNAME", length = 32)
    private String lastname;

    @Column(name = "EMAIL", nullable = false, length = 60)
    private String email;

    @Column(name = "TELEPHONE", length = 50)
    private String telephone;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinTable(name = "USER_ROLE",
            joinColumns = {
                @JoinColumn(name = "USERNAME_ID", referencedColumnName = "USERNAME")},
            inverseJoinColumns = {
                @JoinColumn(name = "ROL_ID", referencedColumnName = "ROL")}
    )
    private Rol role;

    @OneToMany(
            fetch = FetchType.EAGER, mappedBy = "username"
    )
    private Set<UserFileSystem> fileSystem;

表UserFileSystem

@Entity
@Table(name = "USER_FILESYSTEM")
public class UserFileSystem implements Serializable {

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USERNAME", nullable = false)
    private Users username;

    @Id
    @Column(name = "SERVICE_ID", nullable = false, length = 10)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer serviceId;

    @Id
    @Column(name = "SERVICE_FOLDER", nullable = false, length = 64)
    private String serviceFolder;

    @Column(name = "PROTOCOL", length = 20)
    private String protocol;

    @Column(name = "PARAMS", length = 512)
    private String params;

    @OneToMany(
            fetch = FetchType.LAZY, mappedBy = "userFileSystemThumbsPK"
    )
    private Set<UserFileSystemThumbs> fileSystemThumbs;

表UserFileSystemThumbs

@Entity
@Table(name = "USER_FS_THUMBS")
public class UserFileSystemThumbs implements Serializable {

    @EmbeddedId
    private UserFileSystemThumbsPK userFileSystemThumbsPK;

    @Column(name = "SERVICE_URL", nullable = false)
    private String serviceUrl;

    @Column(name = "CONTENT", nullable = false)
    private byte[] content;

    @Column(name = "MIME_TYPE", nullable = false)
    private String mimeType;

主键实体UserFileSystemThumbs

    @Embeddable
    public class UserFileSystemThumbsPK implements Serializable {

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumns({
            @JoinColumn(name = "USERNAME", nullable = false, referencedColumnName = "USERNAME"),
            @JoinColumn(name = "SERVICE_ID", nullable = false, referencedColumnName = "SERVICE_ID")
        })
        private UserFileSystem userFileSystem;

        @Column(name = "SERVICE_URL_HASH", nullable = false)
        private String serviceUrlHash;
Constructor equals hash....

问题我认为UserFileSystemThumbsPK USERNAME列引用了UserFileSystem,但在此表中USERNAME列由用户提供。

1 个答案:

答案 0 :(得分:0)

OP解决方案。

我改变了

@Entity
@Table(name = "USER_FILESYSTEM")
public class UserFileSystem implements Serializable {

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USERNAME", nullable = false)
    private Users username;

    @Id
    @Column(name = "SERVICE_ID", nullable = false, length = 10)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer serviceId;
}

这个

 @Entity
    @Table(name = "USER_FILESYSTEM")
    public class UserFileSystem implements Serializable {

        @EmbeddedId
        private UserFileSystemID userFSID;
}

我创建了具有2个PK值

的UserFileSystemID对象

它像冠军一样工作!