1
我試圖開發使用JPA和Hibernate Spring的啓動應用程序,和我有以下兩個表:JPA /休眠 - 與複合主鍵的外鍵
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;
這兩個表是mappes如下所示: 我使用龍目島避免無聊的東西。 用戶:
@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()
方法。
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>
請顯示您正在使用的查詢或JPA存儲庫類。 –
hibernate具有specyfic列替換,很難理解。打開SQL logginng,名稱將在完整上下文中 –
添加了兩個存儲庫,但它們不是問題。 – LppEdd