JPA:复合主键

时间:2016-08-12 10:45:23

标签: sql hibernate jpa spring-data-jpa composite-primary-key

请我尝试通过审核找到测验(使用spring数据jpa)并且必须隐式搜索具有复合键的quizSubCategory,这里是类:

测验实体:

@Entity
@Table(name = "quiz")
public class Quiz {
@EmbeddedId
private QuizCK id;

@Column
private String title;

@Column
private String status;

@Column
private String state;

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

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

@Column
private String reference;

@Column
private String results;

@Column(name = "is_default")
private boolean isDefault;

@ManyToOne
@JoinColumn(name = "id_audit")
private Audit audit;

@ManyToOne
@JoinColumns({
        @JoinColumn(name = "id_quiz_sub_category", insertable = false, updatable = false),
        @JoinColumn(name = "id_language", insertable = false, updatable = false)
})
private QuizSubCategory quizSubCategory;

@MapsId("idLanguage")
@ManyToOne
@JoinColumn(name="id_language", insertable = false, updatable = false)
private Language language;

public Quiz() {


}

// constructor & getters & setters
}

QuizSubCategory实体:

Entity
@Table(name = "quiz_sub_category")
public class QuizSubCategory implements Serializable {

@EmbeddedId
private QuizSubCategoryCK id;

private String libelle;

@ManyToOne
@JoinColumns({
        @JoinColumn(name = "id_quiz_category", insertable = false, updatable = false),
        @JoinColumn(name = "id_language", insertable = false, updatable = false)
})
private QuizCategory quizCategory;

@MapsId("idLanguage")
@ManyToOne
@JoinColumn(name="id_language", insertable = false, updatable = false)
private Language language;

public QuizSubCategory() {
    super();
}

//constructor & getters & setters
}

QuizSubCategoryCK(主键):

@Embeddable
public class QuizSubCategoryCK implements Serializable {

@Column(name = "id_quiz_sub_category", insertable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int idQuizSubCategory;

@Column(name = "id_language", insertable = false, updatable = false)
private int idLanguage;

public QuizSubCategoryCK() {
}


public int getIdQuizSubCategory() {
    return idQuizSubCategory;
}

public void setIdQuizSubCategory(int idQuizSubCategory) {
    this.idQuizSubCategory = idQuizSubCategory;
}

public int getIdLanguage() {
    return idLanguage;
}

public void setIdLanguage(int idLanguage) {
    this.idLanguage = idLanguage;
}
}

QuizRepository:

public interface QuizRepository extends JpaRepository<Quiz,QuizCK> {
List<Quiz> findByAuditAndLanguage(Audit audit, Language language);
}

控制器:

@RequestMapping(value = "/quizzes/{auditId}", method = RequestMethod.GET)
public List<QuizBean> listQuizzes(@PathVariable int auditId){
    AuditBean auditBean = auditService.getAudit(auditId);
    LanguageBean languageBean = languageService.getLanguageById(1);
    List<QuizBean> quizzes = quizService.findByAudit(AuditMapper.fromBean(auditBean),LanguageMapper.fromBean(languageBean));
    return quizzes;
}

我收到以下错误:

Unable to find jpa.entity.QuizSubCategory with id jpa.entity.QuizSubCategoryCK@79a59c48; nested exception is javax.persistence.EntityNotFoundException: Unable to find jpa.entity.QuizSubCategory with id jpa.entity.QuizSubCategoryCK@79a59c48 

有任何提示吗?!

0 个答案:

没有答案