如何在 Hibernate 中持久保存非实体子类中的实体

发布于 2024-12-05 01:38:33 字数 1291 浏览 2 评论 0原文

我试图将一个实体扩展为一个非实体,用于填充超类的字段。问题是,当我尝试保存它时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏の忆 2024-12-12 01:38:33

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

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);

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);
絕版丫頭 2024-12-12 01:38:33

您不应该扩展实体类,除非要创建更专门的实体类。与实体相关的注释保留在子类中,因此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文