Hibernate:多个表,同一个对象

huangapple go评论111阅读模式
英文:

Hibernate: Multiple tables, same object

问题

我正在尝试编写一个简单的应用程序,其中包含一个表,用于跟踪每个用户所做的付款,以及第二个表,其中包含每个用户支付的总金额(所有付款的总和)。目前,这两个表具有相同的字段(firstName、lastName、amount),并且我已经从同一个Java类进行了映射,但我在将该类映射到多个表时遇到了问题。是否有任何简单的解决方案?

  1. @Entity
  2. @Table(name="Payment")
  3. public class Payment{
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private Long id;
  7. @Column
  8. @NotNull
  9. private String firstName;
  10. @Column
  11. @NotNull
  12. private String lastName;
  13. @Column
  14. @NotNull
  15. private double amount;
  16. ... 构造函数gettersetter
  17. }
英文:

I am trying to write a simple app that contains one table that keeps track of payments each user has made and a second table that contains total amount each user has paid (sum of all payments). Currently, both tables have the same fields (firstName, lastName, amount) and I have mapped them from the same Java class
and I have trouble mapping that class to multiple tables. Is there any simple solution to this?

  1. @Entity
  2. @Table(name="Payment")
  3. public class Payment{
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private Long id;
  7. @Column
  8. @NotNull
  9. private String firstNname;
  10. @Column
  11. @NotNull
  12. private String lastNname;
  13. @Column
  14. @NotNull
  15. private double amount;
  16. ... Constructor, getters and setters
  17. }

答案1

得分: 1

你可以尝试使用 @SecondaryTable annotation

类似于以下内容:

  1. @Entity
  2. @Table(name = "MY_PAYMENT")
  3. @SecondaryTable(name = "MY_PAYMENT_DETAILS",
  4. pkJoinColumns = @PrimaryKeyJoinColumn(name = "pd_payment_id"))
  5. public class Payment {
  6. @Id
  7. @Column(name = "p_id")
  8. private Long id;
  9. @Column(name = "p_first_name")
  10. private String firstNname;
  11. // ...
  12. @Column(name = "pd_amount", table = "MY_PAYMENT_DETAILS")
  13. private double amount;
  14. }

这假设你有以下的数据库模式:

  1. create table MY_PAYMENT
  2. (
  3. p_id number,
  4. p_first_name varchar(200),
  5. CONSTRAINT MY_PAYMENT_PK PRIMARY KEY(p_id)
  6. );
  7. create table MY_PAYMENT_DETAILS
  8. (
  9. pd_payment_id number,
  10. pd_amount number,
  11. CONSTRAINT MY_PAYMENT_DETAILS_PK PRIMARY KEY(pd_payment_id),
  12. CONSTRAINT MY_PAYMENT_DETAILS_FK foreign key(pd_payment_id) references MY_PAYMENT(p_id)
  13. );

还可以参考 hibernate 文档的这个部分。

英文:

You can try to use @SecondaryTable annotation.

Something like this:

  1. @Entity
  2. @Table(name = "MY_PAYMENT")
  3. @SecondaryTable(name = "MY_PAYMENT_DETAILS",
  4. pkJoinColumns = @PrimaryKeyJoinColumn(name = "pd_payment_id"))
  5. public class Payment {
  6. @Id
  7. @Column(name = "p_id")
  8. private Long id;
  9. @Column(name = "p_first_name")
  10. private String firstNname;
  11. // ...
  12. @Column(name = "pd_amount", table = "MY_PAYMENT_DETAILS")
  13. private double amount;
  14. }

This is assumed that you have the following schema:

  1. create table MY_PAYMENT
  2. (
  3. p_id number,
  4. p_first_name varchar(200),
  5. CONSTRAINT MY_PAYMENT_PK PRIMARY KEY(p_id)
  6. );
  7. create table MY_PAYMENT_DETAILS
  8. (
  9. pd_payment_id number,
  10. pd_amount number,
  11. CONSTRAINT MY_PAYMENT_DETAILS_PK PRIMARY KEY(pd_payment_id),
  12. CONSTRAINT MY_PAYMENT_DETAILS_FK foreign key(pd_payment_id) references MY_PAYMENT(p_id)
  13. );

See also this section of hibernate documentation.

答案2

得分: 1

你需要:

@MappedSuperclass
public class ClassWithTheFields{
\\没有用 @Entity 注解
@Id private Integer Id;
...
}

@Entity
public class EntityClass extends ClassWithTheFields{}

@Entity
public class AnotherEntityClass extends ClassWithTheFields{}

这样,继承 ClassWithTheFields 的这两个类将拥有相同的字段,但会映射到不同的数据库表格。

你只需要将所有共同的字段放在一个带有 @MappedSuperclass 注解的类中,但不要用 @Entity 注解标注它,然后在其他带有 @Entity 注解的类中继承这个类。

英文:

You need:

@MappedSuperclass
public class ClassWithTheFields{
\\Not annotated with @Entity
@Id private Integer Id;
...
}

@Entity
public class EntityClass extends ClassWithTheFields{}

@Entity
public class AnotherEntityClass extends ClassWithTheFields{}

This way, both classes extending ClassWithTheFields will have the same fields, but will be mapping different tables.

You just need to put all the common fields in a class annotated with @MappedSuperclass, but not@Entity, and then extend this class in other classes annotated with @Entity.

答案3

得分: 1

你应该选择 @MappedSuperclass。这对你来说是最简单的选择。

英文:

You should go with @MappedSuperclass. It's the easiest choice for you.

huangapple
  • 本文由 发表于 2020年9月12日 19:01:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63859618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定