JPA中三个实体之间的一对多映射

时间:2016-06-20 09:31:23

标签: java mysql jpa spring-boot

我想在三个实体之间构建多对一关系,即用户对板(多个)和Board to Cards(许多),即用户可以拥有许多板,并且板可以有许多卡。我正在使用Spring JPA并自动创建数据库模式。我已正确设置属性,但我无法正确连接它们,因此可以自动构建db模式。以下是我的模型课程,请帮忙: -

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "userid")
    private Long id;

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

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

    @OneToMany(fetch =  FetchType.LAZY ,mappedBy = "username" , targetEntity = Board.class)
    private List<Board> boards;

}

董事会成员

@Entity
@Table(name = "board")
public class Board {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "boardid")
    private Long id;

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

    @Column(name = "boardcreationdate")
    private DateTime boardCreationDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userid", referencedColumnName = "userid")
    private User username;

    @OneToMany(fetch =  FetchType.LAZY ,mappedBy = "boardname" , targetEntity = Board.class)
    private Set<Card> cards;

}

卡类

@Entity
@Table(name = "card")
public class Card {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "cardid")
    private Long id;

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

    @Column(name = "cardcreationdate")
    private DateTime cardCreationDate;

    @Column(name = "cardtimeondone")
    private DateTime cardTimeOnDone;

    @Column(name = "cardlife")
    private DateTime cardLife;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "boardid", referencedColumnName = "boardid")
    private Board boardname;

}

这是带有一些属性的application.properties文件。

spring.datasource.initialize=true
spring.datasource.platform=mysql
spring.datasource.time-between-eviction-runs-millis=34000
spring.datasource.min-evictable-idle-time-millis=55000
spring.datasource.max-wait=28800000
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql=true

它正在创建架构,但会产生如下错误: -

        2016-06-20 15:12:37.288 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    alter table board 
        drop 
        foreign key FKbon6omt9qov9iduumkuj117xw
2016-06-20 15:12:37.357 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    alter table board 
        drop 
        foreign key FKcefyg893q4c26nvw7uus84ts3
2016-06-20 15:12:37.386  WARN 8772 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Unable to execute command [
    alter table board 
        drop 
        foreign key FKcefyg893q4c26nvw7uus84ts3]

org.hibernate.tool.schema.spi.CommandAcceptanceException: Unable to execute command [
    alter table board 
        drop 
        foreign key FKcefyg893q4c26nvw7uus84ts3]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:63) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:370) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:355) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applyConstraintDropping(SchemaDropperImpl.java:327) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:229) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:153) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:125) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:111) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:137) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:64) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:458) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:881) [hibernate-entitymanager-5.1.0.Final.jar:5.1.0.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) [spring-orm-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338) [spring-orm-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) [spring-orm-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) [spring-orm-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) [spring-context-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) [spring-context-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) [spring-context-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1182) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1171) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at com.causecode.challenge.TrellocardlifeApplication.main(TrellocardlifeApplication.java:14) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Can't DROP 'FKcefyg893q4c26nvw7uus84ts3'; check that column/key exists
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_25]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[na:1.8.0_25]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.Util.getInstance(Util.java:381) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:734) ~[mysql-connector-java-5.1.6.jar:na]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:51) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    ... 38 common frames omitted

2016-06-20 15:12:37.392 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    alter table card 
        drop 
        foreign key FKgicrq63fj0nx3bh6wlncf235i
2016-06-20 15:12:37.424 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    drop table if exists board
2016-06-20 15:12:37.437 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    drop table if exists card
2016-06-20 15:12:37.448 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    drop table if exists user
2016-06-20 15:12:37.468 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    create table board (
        boardid bigint not null auto_increment,
        boardcreationdate tinyblob,
        boardname varchar(255),
        userid bigint,
        primary key (boardid)
    )
2016-06-20 15:12:37.539 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    create table card (
        id bigint not null auto_increment,
        cardcreationdate tinyblob,
        cardlife tinyblob,
        card_name varchar(255),
        cardtimeondone tinyblob,
        boardid bigint,
        primary key (id)
    )
2016-06-20 15:12:37.600 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    create table user (
        userid bigint not null auto_increment,
        password varchar(255),
        username varchar(255),
        primary key (userid)
    )
2016-06-20 15:12:37.631 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    alter table board 
        add constraint FKbon6omt9qov9iduumkuj117xw 
        foreign key (userid) 
        references user (userid)
2016-06-20 15:12:37.752 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    alter table board 
        add constraint FKcefyg893q4c26nvw7uus84ts3 
        foreign key (boardname) 
        references board (boardid)
2016-06-20 15:12:37.812  WARN 8772 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Unable to execute command [
    alter table board 
        add constraint FKcefyg893q4c26nvw7uus84ts3 
        foreign key (boardname) 
        references board (boardid)]

org.hibernate.tool.schema.spi.CommandAcceptanceException: Unable to execute command [
    alter table board 
        add constraint FKcefyg893q4c26nvw7uus84ts3 
        foreign key (boardname) 
        references board (boardid)]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:63) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:423) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlStrings(SchemaCreatorImpl.java:408) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromMetadata(SchemaCreatorImpl.java:359) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:165) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:134) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:120) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:148) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:64) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:458) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465) [hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:881) [hibernate-entitymanager-5.1.0.Final.jar:5.1.0.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) [spring-orm-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338) [spring-orm-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) [spring-orm-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) [spring-orm-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) [spring-context-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) [spring-context-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) [spring-context-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1182) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1171) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at com.causecode.challenge.TrellocardlifeApplication.main(TrellocardlifeApplication.java:14) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
Caused by: java.sql.SQLException: Cannot add foreign key constraint
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465) ~[mysql-connector-java-5.1.6.jar:na]
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:734) ~[mysql-connector-java-5.1.6.jar:na]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:51) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    ... 37 common frames omitted

2016-06-20 15:12:37.814 DEBUG 8772 --- [           main] org.hibernate.SQL                        : 
    alter table card 
        add constraint FKgicrq63fj0nx3bh6wlncf235i 
        foreign key (boardid) 
        references board (boardid)
2016-06-20 15:12:37.900  INFO 8772 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@329bad59'
2016-06-20 15:12:37.954  INFO 8772 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2016-06-20 15:12:39.191  INFO 8772 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-06-20 15:12:39.200  INFO 8772 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-06-20 15:12:39.305  INFO 8772 --- [           main] c.c.challenge.TrellocardlifeApplication  : Started TrellocardlifeApplication in 8.172 seconds (JVM running for 8.914)
2016-06-20 15:12:39.306  INFO 8772 --- [       Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7fad8c79: startup date [Mon Jun 20 15:12:31 IST 2016]; root of context hierarchy
2016-06-20 15:12:39.308  INFO 8772 --- [       Thread-3] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2016-06-20 15:12:39.310  INFO 8772 --- [       Thread-3] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2016-06-20 15:12:39.310  INFO 8772 --- [       Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

Process finished with exit code 0

我认为实体类只有一些问题。请帮我定义一个正确的模型并在mysql中自动创建它们。

2 个答案:

答案 0 :(得分:2)

尝试改变这种方式:

@Entity
@Table(name = "user")
public class User {
  @OneToMany(mappedBy = "user")
  private List<Board> boards;

}
@Entity
@Table(name = "board")
public class Board {
    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name = "userid")
    private User user;

    @OneToMany(mappedBy = "board")
    private Set<Card> cards;
}
@Entity
@Table(name = "card")
public class Card {

    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name = "boardid")
    private Board board;

}

答案 1 :(得分:1)

不确定这是否解决了您的错误,但这看起来无效

 @OneToMany(fetch =  FetchType.LAZY ,mappedBy = "boardname" , targetEntity = Board.class)
    private Set<Card> cards;

应该是

 @OneToMany(fetch =  FetchType.LAZY ,mappedBy = "boardname" , targetEntity = Card.class)
    private Set<Card> cards;