【发布时间】:2018-11-16 05:11:52
【问题描述】:
我正在使用 spring boot 和 jpa 创建表,但我需要创建没有 @Id 主键列的表。没有这个 @Id 字段我无法创建表。如何使用 spring data jpa 得到这个?
LetterDoc.java
package com.ashwin.springsecurityangular.model;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "letter_doc")
public class LetterDoc implements Serializable {
private static final long serialVersionUID = 1L;
@ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
@JoinColumn(name = "letterNo")
@JsonIgnore
private Letter letter;
@ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
@JoinColumn(name = "documentId")
@JsonIgnore
private Document document;
private String docFile;
//i omitted getters and setters and both constructor
}
它问我@Id 字段需要。所以出现如下错误:
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 org.hibernate.AnnotationException: No identifier specified for entity: com.ashwin.springsecurityangular.model.LetterDoc
在这张表中,我避免使用@Id 字段,但它没有给我。如何实现这一点?
【问题讨论】:
-
JPA ID 不必对应于数据库 PK - 它只需是唯一列或列组合。如果您有 2 列形成唯一 ID,那么您可以使用复合 JPA ID。 en.wikibooks.org/wiki/Java_Persistence/…
标签: java spring hibernate spring-boot jpa