gpt4 book ai didi

java - @SqlResultSetMapping 列 : entities with sub-entities

转载 作者:搜寻专家 更新时间:2023-10-30 23:02:26 26 4
gpt4 key购买 nike

我有测试实体:

public class Test {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "duration", nullable = false)
private int duration;
@Column(name = "test_name", nullable = false, unique = true)
private String testName;
@Column(name = "archived", nullable = false)
private boolean archived;
@OneToMany(mappedBy = "test", fetch = FetchType.EAGER)
private Set<Question> questions;
@ManyToMany(mappedBy = "tests")
private Set<User> users;

问题实体:

public class Question {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "is_multichoice", nullable = false)
private boolean isMultichoice;
@Column(name = "is_open", nullable = false)
private boolean isOpen;
@Column(name = "picture")
private String picture;
@Column(name = "question")
private String question;
@ManyToOne
@JoinColumn(name = "test_id", nullable = false)
private Test test;
@Column(name = "archived", nullable = false)
private boolean isArchived;
@OneToMany(mappedBy = "question", fetch = FetchType.EAGER)
private Set<Answer> answers;

这个测试实体问题集,这样问题实体答案集

我编写了 SQL 查询来进行测试(它不是 HQL 的原因你可以通过链接 Hibernate HQL : no entity found for query 找到):

@NamedNativeQuery(name = "getCurrentTestById",
query = "SELECT t.id as tId, t.test_name, t.duration, q.id as qId, " +
"q.question as question, q.is_multichoice as is_multichoice, " +
"q.is_open as is_open, a.id as aId, a.answer_text as answer_text FROM result r " +
"JOIN test t ON r.test_id = t.id " +
"JOIN user u ON r.user_id = u.id " +
"JOIN question q ON t.id = q.test_id JOIN answer a ON q.id = a.question_id " +
"WHERE t.id = :testId AND u.id = :userId AND r.permission = :permissionId " +
"AND q.archived = false AND a.archived = false")

现在我需要使用@SqlResultSetMapping 注释将它映射到我的实体测试:

@SqlResultSetMappings({
@SqlResultSetMapping(name="toTest",
entities = {
@EntityResult(entityClass = com.bionic.entities.Test.class, fields = {
@FieldResult(name = "id", column = "tId"),
@FieldResult(name = "test_name", column = "test_name"),
@FieldResult(name = "duration", column = "duration"),
@FieldResult(name = "questions.question", column = "question"),
@FieldResult(name = "questions.id", column = "qId"),
@FieldResult(name = "questions.isMultichoice", column = "is_multichoice"),
@FieldResult(name = "questions.isOpen", column = "is_open"),
@FieldResult(name = "questions.answers.id", column = "aId"),
@FieldResult(name = "questions.answers.answer_text", column = "answer_text"),
})
})
})

我遇到异常:

Caused by: org.hibernate.MappingException: dotted notation reference neither a component nor a many/one to one

最佳答案

这就是为什么框架通常是坏消息。而不是使用hibernate,你应该遵循接口(interface)隔离原则。您的应用程序不应该知道或关心如何选择您需要的数据以及表名是什么等。只需创建一个存储过程来承担此责任并调用它,而不是在您的应用程序中包含所有垃圾代码。然后,如果您想要一种简单的映射方法,只需在最后调用 For JSON 让您的存储过程返回 json。将对象字段映射到 JSON 对象变得轻而易举。您会发现,使用框架,您花在框架故障排除上的时间比实际编程的时间还多。

关于java - @SqlResultSetMapping 列 : entities with sub-entities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com