如何在 Hibernate 中持久保存非实体子类中的实体
我试图将一个实体扩展为一个非实体,用于填充超类的字段。问题是,当我尝试保存它时,Hibernate 会抛出 MappingException。这是因为即使我将 ReportParser 转换为 Report,运行时实例仍然是一个 ReportParser,因此 Hibernate 抱怨它是一个未知实体。
@Entity
@Table(name = "TB_Reports")
public class Report
{
Long id;
String name;
String value;
@Id
@GeneratedValue
@Column(name = "cReportID")
public Long getId()
{
return this.id;
}
public void setId(Long id)
{
this.id = id;
}
@Column(name = "cCompanyName")
public String getname()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
@Column(name = "cCompanyValue")
public String getValue()
{
return this.name;
}
public void setValue(String value)
{
this.value = value;
}
}
ReportParser 仅用于填充字段。
public class ReportParser extends report
{
public void setName(String htmlstring)
{
...
}
public void setValue(String htmlstring)
{
...
}
}
尝试将其转换为报告并保存它
...
ReportParser rp = new ReportParser();
rp.setName(unparsed_string);
rp.setValue(unparsed_string);
Report r = (Report)rp;
this.dao.saveReport(r);
在迁移到 ORM 之前我已经使用过这种模式,但我不知道如何使用 Hibernate 来做到这一点。是否可以?
I am trying to extend an entity into a non-entity which is used to fill the superclass's fields. The problem is that when I try to save it Hibernate throws a MappingException. This is because even though I cast ReportParser to Report, the runtime instance is still a ReportParser so Hibernate complains that it is an unknown entity.
@Entity
@Table(name = "TB_Reports")
public class Report
{
Long id;
String name;
String value;
@Id
@GeneratedValue
@Column(name = "cReportID")
public Long getId()
{
return this.id;
}
public void setId(Long id)
{
this.id = id;
}
@Column(name = "cCompanyName")
public String getname()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
@Column(name = "cCompanyValue")
public String getValue()
{
return this.name;
}
public void setValue(String value)
{
this.value = value;
}
}
ReportParser is only used to fill in fields.
public class ReportParser extends report
{
public void setName(String htmlstring)
{
...
}
public void setValue(String htmlstring)
{
...
}
}
Attempt to cast it to a Report and save it
...
ReportParser rp = new ReportParser();
rp.setName(unparsed_string);
rp.setValue(unparsed_string);
Report r = (Report)rp;
this.dao.saveReport(r);
I've used this pattern before I moved to an ORM, but I can't figure out how to do this with Hibernate. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是否绝对有必要对实体进行子类化?您可以使用构建器模式:
Is it absolutely necessary to subclass the entity? You could use the builder pattern:
您不应该扩展实体类,除非要创建更专门的实体类。与实体相关的注释保留在子类中,因此 Hibernate 会感到困惑。
将业务逻辑放入实体类中也是非常有争议的 — 您必须意识到 JPA 实现者(例如 Hibernate)可能(并且通常)通过生成的代理运行您的 getter/setter。内部逻辑复杂,可能会遇到难以追踪的问题。
You're not supposed to extend entity classes, unless to make more specialized entity classes. Entity-related annotations are retained in the subclass, thus Hibernate gets confused.
Putting business logic in entity classes is also highly debatable — you have to be aware that JPA implementors, like Hibernate, may (and usually do) run your getters/setters through generated proxies. With complicated logic inside you may come across problems that are difficult to trace.