对于此实体层次结构,什么是好的持久性设计? [英] What is a good persistence design for this entity hierarchy?
问题描述
我是JPA的新手,正在尝试为以下课程设计设计.所有类都具有equals和覆盖的hashcode,getters和setter以及空的构造函数.
I'm new to JPA and trying to work out the design for the following classes. All classes have equals and hashcode overriden, getters and setter and empty constructor.
我对所有实体都有一个基类:
I have a base class for all entities:
public abstract class BaseEntity {
protected Point loc;
protected List<Property> properties = new ArrayList<>();
}
Point类只是标准的x-y持有人
The Point class is just the standard x-y holder
public class Point {
private int x, y;
}
Property类包含一个名称和一个id:
And the Property class holds a name and an id:
public class Property {
private int id;
private String name;
}
有3个类继承自BaseEntity
.第一个是House.一所房子可以容纳一个单人家庭.
There are 3 classes that inherit from BaseEntity
. The first one is House. A House can hold a single family.
public class House extends BaseEntity {
private Family family;
}
public class Tree extends BaseEntity {
private String name;
}
家庭很快就会出现.第二个是所有生物实体的超类:
Family will be show soon. The second one is the superclass for all the living entities:
public abstract class LivingEntity extends BaseEntity {
public static enum Status { ALIVE, DECEASED }
int id;
Status status;
}
其中
public class Family extends LivingEntity {
private House house;
private List<Minion> members;
}
和
public class Member extends LivingEntity {
private Family family;
private String name;
}
所以层次图是
BaseEntity
|
/|\
/ | \
/ | \
House Tree LivingEntity
|
/ \
Family Member
组成图是
House <---> Family ---> {Member, Member, Member, ...}
^ | | |
|----------------------------
除了继承的内容.
房屋和树木通过其位置(在equals方法中)进行标识,因此我认为我可以将其用作@Id
.我可以给他们一个特定的id
字段,但这很多余.
House and Tree are identified by their location (in the equals method) so I think I can use that as the @Id
. I could give them a specific id
field but it's redundant.
这意味着我还需要使Point成为一个具有x和y组合ID的实体.我不知道这是否会导致同一点在数据库中被保存多次.我将保存不同的BaseEntities集,因此,如果2个在同一Point上,它将复制该Point条目还是将全部引用到一个地方?
This means that I need to make Point an entity also with a composite ID from its x and y. I don't know if this will cause the same point to be saved multiple times across the DB. I will have different sets of BaseEntities saved, so if 2 are at the same Point will it duplicate the point entry or refer all to one place?
家庭和成员由其ID定义,因为他们可以在一个房子之间移动.因此,我想在LivingEntity中使用id
作为@Id
,但它会与BaseEntities中的@Id
发生冲突.
Family and Member are defined by their id because they can move from house to house. So I want to use id
in LivingEntity as the @Id
but I it will cause a conflict with the @Id
from BaseEntities.
这是我的注释:
@MappedSuperclass
public abstract class BaseEntity {
@Id
protected Point loc;
@OneToMany
protected List<Property> effects = new ArrayList<>();
}
@Entity
@IdClass(value = PointID.class)
public class Point {
@Id
private int x, y;
}
class PointID {
int x, y;
}
@Entity
public class Property {
@Id
protected int id;
protected String name;
}
@Entity
public class House extends BaseEntity {
@OneToOne
private Family family;
}
@Entity
public class Tree extends BaseEntity {
private String name;
}
@MappedSuperclass
public abstract class LivingEntity extends BaseEntity {
public static enum Status { ALIVE, DECEASED }
@Id
int id;
@Enumerated(EnumType.STRING)
Status status;
}
@Entity
public class Family extends LivingEntity {
@OneToOne // bidirectional? specify mappedBy?
private House house;
@OneToMany
private List<Minion> members;
}
@Entity
public class Member extends LivingEntity {
@ManyToOne // bidirectional? specify mappedBy?
private Family family;
private String name;
}
除了2个@MappedSuperclass的冲突外,我不知道我是否以正确的方式正确地使用了注释.对于这种情况,什么是好的设计?
Except for the conflict of the 2 @MappedSuperclass I don't know if I'm using the annotations correctly and in a smart way. What would be a good design for this case?
推荐答案
我不知道您的业务原因(对整个家庭而不是单个成员拥有活着"或已故"身份似乎很奇怪),但对于给出的示例,使用类似以下内容似乎更现实:
I don't know your business reasons (it seems strange to have a an 'alive' or 'deceased' status for an entire family instead of just individual members), but for the example given, it seems more realistic to have something like:
BaseFixedObject -> Point
|
/|
/ |
/ |
House Tree
LivingEntity -> many Properties
|
/|
/ |
/ |
Member |
Family -> many Members
Property -> many BaseFixedObjects
于是,财产将主要是财产的集合-带有院子的房屋,其中可能有许多树木.我不知道它是否会引用该点,或者每个树/房屋都有自己的点-这取决于您的需要.或者,您可以取消Property,让LivingEntity直接引用BaseFixedObjects.
Property would then be mostly a collection of assets on a property - A house with a yard that may have many trees. I don't know if it would reference the Point, or each tree/house has its own point - it depends on your need. Or you can do away with Property and have LivingEntity reference BaseFixedObjects directly.
这些关系可能是双向的,因此属性可以引用与其绑定的LivingEntity,该实体可以是单个成员"或家庭.我不确定在这种模式下是否需要家庭以外的共同所有权- 如果是这样,一个财产可能需要允许多个所有者,从而使其成为ManyToMany关系.
The relationships might be bidirectional, so a property may reference the LivingEntity that it is tied to, which would be a single 'member' or Family. I'm not sure if joint ownership is required outside of families in this model - if it is, a Property might need to allow more than one owner, making it a ManyToMany relationship.
这样,您的家人(可能)拥有的房屋具有固定的位置.它不需要引用点本身,因为您可以通过关联的属性对其进行访问.
This way, your family (likely) has a house that has a fixed point. It doesn't need to reference the point itself, as you have access to it through the associated properties.
这篇关于对于此实体层次结构,什么是好的持久性设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!