JPA / Hibernate - 具有外键的复合主键

时间:2017-08-24 19:57:21

标签: java spring hibernate jpa spring-boot

我尝试使用JPA和Hibernate开发Spring Boot应用程序,并且我有以下两个表:

CREATE TABLE IF NOT EXISTS `library`.`User` (
  `id` VARCHAR(30) NOT NULL COMMENT 'The username',
  `name` VARCHAR(50) NULL,
  `surname` VARCHAR(50) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `library`.`Library` (
  `userId` VARCHAR(30) NOT NULL,
  `id` CHAR(36) NOT NULL COMMENT 'An autogenerated UUID',
  `name` VARCHAR(50) NULL,
  PRIMARY KEY (`id`, `userId`),
  INDEX `USER_idx` (`userId` ASC),
  CONSTRAINT `USER`
    FOREIGN KEY (`userId`)
    REFERENCES `library`.`User` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

这两张表格如下: 我使用龙目岛来避免无聊的东西。 用户:

@Entity
@Table(name = "user")
@NoArgsConstructor
@Getter
@Setter
public class User
{
   @Id
   @Column(name = "id")
   private String id;

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

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

   @OneToMany(mappedBy = "userId")
   private Set<Library> libraries = new HashSet<>();
}

库:

@Entity
@Table(name = "library")
@IdClass(LibraryId.class)
@NoArgsConstructor
@Getter
@Setter
public class Library
{
   @Id
   @ManyToOne
   @JoinColumn(name = "userId", referencedColumnName = "id")
   private User userId;

   @Id
   @Column(name = "id")
   private String id;

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

图书馆的复合ID:

@RequiredArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class LibraryId implements Serializable
{
   @NonNull
   public String userId;

   @NonNull
   public String id;
}

我在这里阅读了所有内容https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing 当通过Spring Repository findAll()方法执行查询时,Spring / Hibernate仍会返回此错误。

SQL Error: 1054, SQLState: 42S22
Unknown column 'libraries0_.user_id' in 'field list'

出了什么问题?我失去了一天,但我还是不理解。

根据要求,这些是两个存储库:

@Repository
public interface UserRepository extends JpaRepository<User, String>
{
   //
}

@Repository
public interface LibraryRepository extends JpaRepository<Library, LibraryId>
{
   //
}

实际查询:

select user0_.id as id1_1_, user0_.name as name2_1_, user0_.surname as surname3_1_ from user user0_

select libraries0_.user_id as user_id2_0_0_, libraries0_.id as id1_0_0_, libraries0_.id as id1_0_1_, libraries0_.user_id as user_id2_0_1_, libraries0_.name as name3_0_1_ from library libraries0_ where libraries0_.user_id=?

好像Hibernate没有加入...

根据要求,这是Maven POM:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

1 个答案:

答案 0 :(得分:0)

啊,啊,终于旅程结束了...... See this question
问题是转换实体的列名时的Hibernate命名策略。

使用:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

解决了这个问题。但 我不明白为什么@JoinColumn(name = "userId")无法识别!