英文:
Hibernate: Multiple tables, same object
问题
我正在尝试编写一个简单的应用程序,其中包含一个表,用于跟踪每个用户所做的付款,以及第二个表,其中包含每个用户支付的总金额(所有付款的总和)。目前,这两个表具有相同的字段(firstName、lastName、amount),并且我已经从同一个Java类进行了映射,但我在将该类映射到多个表时遇到了问题。是否有任何简单的解决方案?
@Entity
@Table(name="Payment")
public class Payment{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
@NotNull
private String firstName;
@Column
@NotNull
private String lastName;
@Column
@NotNull
private double amount;
... 构造函数,getter和setter
}
英文:
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?
@Entity
@Table(name="Payment")
public class Payment{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
@NotNull
private String firstNname;
@Column
@NotNull
private String lastNname;
@Column
@NotNull
private double amount;
... Constructor, getters and setters
}
答案1
得分: 1
你可以尝试使用 @SecondaryTable
annotation。
类似于以下内容:
@Entity
@Table(name = "MY_PAYMENT")
@SecondaryTable(name = "MY_PAYMENT_DETAILS",
pkJoinColumns = @PrimaryKeyJoinColumn(name = "pd_payment_id"))
public class Payment {
@Id
@Column(name = "p_id")
private Long id;
@Column(name = "p_first_name")
private String firstNname;
// ...
@Column(name = "pd_amount", table = "MY_PAYMENT_DETAILS")
private double amount;
}
这假设你有以下的数据库模式:
create table MY_PAYMENT
(
p_id number,
p_first_name varchar(200),
CONSTRAINT MY_PAYMENT_PK PRIMARY KEY(p_id)
);
create table MY_PAYMENT_DETAILS
(
pd_payment_id number,
pd_amount number,
CONSTRAINT MY_PAYMENT_DETAILS_PK PRIMARY KEY(pd_payment_id),
CONSTRAINT MY_PAYMENT_DETAILS_FK foreign key(pd_payment_id) references MY_PAYMENT(p_id)
);
还可以参考 hibernate 文档的这个部分。
英文:
You can try to use @SecondaryTable
annotation.
Something like this:
@Entity
@Table(name = "MY_PAYMENT")
@SecondaryTable(name = "MY_PAYMENT_DETAILS",
pkJoinColumns = @PrimaryKeyJoinColumn(name = "pd_payment_id"))
public class Payment {
@Id
@Column(name = "p_id")
private Long id;
@Column(name = "p_first_name")
private String firstNname;
// ...
@Column(name = "pd_amount", table = "MY_PAYMENT_DETAILS")
private double amount;
}
This is assumed that you have the following schema:
create table MY_PAYMENT
(
p_id number,
p_first_name varchar(200),
CONSTRAINT MY_PAYMENT_PK PRIMARY KEY(p_id)
);
create table MY_PAYMENT_DETAILS
(
pd_payment_id number,
pd_amount number,
CONSTRAINT MY_PAYMENT_DETAILS_PK PRIMARY KEY(pd_payment_id),
CONSTRAINT MY_PAYMENT_DETAILS_FK foreign key(pd_payment_id) references MY_PAYMENT(p_id)
);
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论