如何在Hibernate中坚持一个来自非实体子类的实体 [英] How to persist an entity from an non-entity subclass in Hibernate

查看:119
本文介绍了如何在Hibernate中坚持一个来自非实体子类的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个实体扩展成一个用于填充超类字段的非实体。问题是,当我尝试保存它时,Hibernate会抛出一个MappingException。这是因为即使我将ReportParser投影到Report,运行时实例仍然是ReportParser,因此Hibernate抱怨它是未知实体。

  @Entity 
@Table(name =TB_Reports)
public class Report
{
Long id;
字符串名称;
字符串值;

@Id
@GeneratedValue
@Column(name =cReportID)
public Long getId()
{
return this。 ID;
}

public void setId(Long id)
{
this.id = id;

$ b $ @Column(name =cCompanyName)
public String getname()
{
return this.name;
}

public void setName(String name)
{
this.name = name;

$ b $ @Column(name =cCompanyValue)
public String getValue()
{
return this.name;
}

public void setValue(String value)
{
this.value = value;




$ b ReportParser仅用于填写字段。 p>

  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);
报告r =(报告)rp;
this.dao.saveReport(r);

在我转移到ORM之前,我已经使用了这种模式,但我无法弄清楚用Hibernate来做到这一点。是否有可能?

解决方案

实体的子类是否绝对有必要?您可以使用构建器模式:

  public class ReportBuilder {
private report report;
public ReportBuilder(){
this.report = new Report();
}
public ReportBuilder setName(String unparsedString){
//解析
report.setName(parsedString);
返回此;
}
public ReportBuilder setValue(String unparsedString){
//解析
report.setValue(parsedString);
返回此;
}
public build(){
return report;



Report report = new ReportBuilder()
.setName(unparsedString)
.setValue(unparsedString)
.build );
dao.saveReport(report);


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?

解决方案

Is it absolutely necessary to subclass the entity? You could use the builder pattern:

public class ReportBuilder {
    private Report report;
    public ReportBuilder() {
        this.report = new Report();
    }
    public ReportBuilder setName(String unparsedString) {
        // do the parsing
        report.setName(parsedString);
        return this;
    }
    public ReportBuilder setValue(String unparsedString) {
        // do the parsing
        report.setValue(parsedString);
        return this;
    }
    public Report build() {
        return report;
    }
}

Report report = new ReportBuilder()
                   .setName(unparsedString)
                   .setValue(unparsedString)
                   .build();
dao.saveReport(report);

这篇关于如何在Hibernate中坚持一个来自非实体子类的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆