@CollectionTable无法连接

时间:2018-04-07 17:36:31

标签: spring jpa

我有两张桌子

用户表

create table users (
    usernumber int unsigned not null auto_increment primary key,
    username varchar(32) not null,
    password varchar(72) not null,
    displayname varchar(32) not null,
    birthday datetime not null,
    email varchar(255) not null,
    firstname varchar(32) not null,
    lastname varchar(32) not null
);

用户锁定表

create table users_locked (
    idx int unsigned not null auto_increment primary key,
    usernumber int unsigned not null,
    start_date datetime not null,
    end_date datetime not null,
    memo varchar(32) default '' not null,
    foreign key (usernumber) references users(usernumber) on delete cascade
);

我用@CollectionTable创建了与两个表相关联的实体

...以下

Users.java

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "users")
@Access(AccessType.FIELD)
public class Users {

    @EmbeddedId
    private UsersID usernumber;

    @Column(name = "username", unique = true)
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "displayname")
    private String displayname;

    @Embedded
    private PersonalInformation personal;

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "users_locked", joinColumns = @JoinColumn(name = "usernumber"))
    private List<LockPeriod> locked;
    // ....
}

LockPeriod.java

@Getter
@Builder
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Embeddable
public class LockPeriod {

    @Column(name = "start_date")
    private Date start;

    @Column(name = "end_date")
    private Date end;

    @Column(name = "memo")
    private String memo;
    //....
}

但发生以下错误且无法映射:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

我做错了吗? 我很抱歉缺乏英语。

0 个答案:

没有答案