简体   繁体   English

此实体层次结构的良好持久性设计是什么?

[英]What is a good persistence design for this entity hierarchy?

I'm new to JPA and trying to work out the design for the following classes. 我是JPA的新手,正在尝试为以下课程设计设计。 All classes have equals and hashcode overriden, getters and setter and empty constructor. 所有类都具有equals和hashcode覆盖,getters和setter以及空构造函数。

I have a base class for all entities: 我对所有实体都有一个基类:

public abstract class BaseEntity {
    protected Point loc;
    protected List<Property> properties = new ArrayList<>();
}

The Point class is just the standard xy holder Point类只是标准的xy持有人

public class Point {
    private int x, y;
}

And the Property class holds a name and an id: 而且Property类拥有一个名称和一个ID:

public class Property {
    private int id;
    private String name;
}

There are 3 classes that inherit from BaseEntity . 有3个类继承自BaseEntity The first one is House. 第一个是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;
}

which includes 包括

public class Family extends LivingEntity {
    private House house;
    private List<Minion> members;
}

and

public class Member extends LivingEntity {
    private Family family;
    private String name;
}

So the hierarchy graph is 所以层次图是

    BaseEntity
        |
       /|\
      / | \
     /  |  \
House Tree  LivingEntity
                |
               / \
         Family   Member

and the composition graph is 组成图是

House <---> Family ---> {Member, Member, Member, ...}
               ^           |       |       |
               |----------------------------

in addition to what they inherit. 除了继承。


House and Tree are identified by their location (in the equals method) so I think I can use that as the @Id . House和Tree通过其位置标识(在equals方法中),因此我认为可以将其用作@Id I could give them a specific id field but it's redundant. 我可以给他们一个特定的id字段,但这是多余的。

This means that I need to make Point an entity also with a composite ID from its x and y. 这意味着我还需要使Point成为一个实体,该实体还具有x和y的复合ID。 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? 我将保存不同的BaseEntities集,因此,如果2个在同一Point上,它将复制该Point条目还是将全部引用到一个地方?

Family and Member are defined by their id because they can move from house to house. 家庭和成员由其ID定义,因为他们可以在一个家庭中移动。 So I want to use id in LivingEntity as the @Id but I it will cause a conflict with the @Id from BaseEntities. 所以我想在LivingEntity中将id用作@Id但它会与BaseEntities中的@Id发生冲突。

Here are the annotations I have: 这是我的注释:

@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;
}

Except for the conflict of the 2 @MappedSuperclass I don't know if I'm using the annotations correctly and in a smart way. 除了2个@MappedSuperclass的冲突外,我不知道我是否以正确的方式正确地使用了注释。 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 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. 或者,您可以取消Property,让LivingEntity直接引用BaseFixedObjects。

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. 这些关系可能是双向的,因此属性可以引用与其绑定的LivingEntity,该实体可以是单个“成员”或家庭。 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. 我不确定在这种模式下是否需要在家庭之外拥有共同所有权-如果是这样,那么某个物业可能需要允许多个所有者,从而使其成为ManyToMany关系。

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. 它不需要引用点本身,因为您可以通过关联的属性对其进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM