Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Hibernate中正确地持久化具有多个父对象的单个子对象?_Java_Spring_Hibernate_Jpa_Orm - Fatal编程技术网

Java 如何在Hibernate中正确地持久化具有多个父对象的单个子对象?

Java 如何在Hibernate中正确地持久化具有多个父对象的单个子对象?,java,spring,hibernate,jpa,orm,Java,Spring,Hibernate,Jpa,Orm,使用hibernate,我有一个在我看来非常复杂的ORM。我有一个子实体类UserStatus,它与两个父实体类Store和Account具有双向关系。见下面的代码: @Entity @Table(name = "accounts") public class **Account** { //**PARENT CLASS 1** @JsonIgnore @Getter @Setter @OneToMany(mappedB

使用hibernate,我有一个在我看来非常复杂的ORM。我有一个子实体类UserStatus,它与两个父实体类Store和Account具有双向关系。见下面的代码:

@Entity
@Table(name = "accounts")
public class **Account** {    //**PARENT CLASS 1**

        @JsonIgnore
        @Getter @Setter
        @OneToMany(mappedBy = "user",
                   cascade = CascadeType.ALL,
                   orphanRemoval = true) //Owning side
        @Where(clause = "NOT active")
        private List<UserStatus> history = new ArrayList<>();

        @Getter @Setter
        @OneToOne(mappedBy = "user",
                  cascade = CascadeType.ALL,
                  orphanRemoval = true)
        @Where(clause = "active")
        private UserStatus status;

        //other account instance variables...
}

@Entity
@Table(name = "stores")
public class **Store** {    //**PARENT CLASS 2**

        @Schema(description = "List of all queued \"Waiting\" user states to this Store, aka, the queue")
        @OneToMany(mappedBy = "store",
                   cascade = CascadeType.PERSIST,
                   orphanRemoval = true) //Owning side
        @Where(clause = "user_state = 'WAITING' AND active")
        @JsonIgnore
        private List<UserStatus> queue = new ArrayList<>();

        @Schema(description = "List of all queued \"Shopping\" user states to this Store, aka, the shoppers")
        @OneToMany(mappedBy = "store",
                   cascade = CascadeType.PERSIST,
                   orphanRemoval = true) //Owning side
        @Where(clause = "user_state = 'SHOPPING' AND active")
        @JsonIgnore
        private List<UserStatus> shoppers = new ArrayList<>();

        //some other Store instance variables, methods, and information
}

@Entity
@Table(name = "user_state")
public class **UserStatus** implements Comparable<UserStatus>{    //**Child Class**

        @Id
        @GeneratedValue
        @Column(name = "id")
        private int id;

        @Column(name = "user_state", columnDefinition = "enum('IDLE', 'WAITING', 'SHOPPING')")
        private String state;

        @Column(name = "time_stamp")
        private Timestamp datetime;

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "user_id", nullable = false) //**Bidirectional child-parent relationship **
        private Account user;

        @ManyToOne
        @JoinColumn(name = "store_id", nullable = false) //inverse side (Store is the owning side) //**Bidirectional child-parent relationship **
        private Store store;

        @Schema(description = "Indicates whether this status is currently active or not, aka history")
        @JsonIgnore
        private boolean active;

        //other methods...
}
@实体
@表(name=“账户”)
公共类**帐户**{//**父类1**
@杰索尼奥雷
@Getter@Setter
@OneToMany(mappedBy=“用户”,
cascade=CascadeType.ALL,
孤立删除=真)//拥有方
@其中(第条=“非活动”)
私有列表历史记录=新建ArrayList();
@Getter@Setter
@OneTONE(mappedBy=“用户”,
cascade=CascadeType.ALL,
(删除=真)
@其中(第条“有效”)
私有用户状态;
//其他帐户实例变量。。。
}
@实体
@表(name=“stores”)
公共类**存储**{//**父类2**
@架构(description=“此存储的所有排队\“等待\”用户状态列表,也称为队列”)
@OneToMany(mappedBy=“商店”,
cascade=CascadeType.PERSIST,
孤立删除=真)//拥有方
@其中(子句=“用户状态=‘等待’和活动”)
@杰索尼奥雷
私有列表队列=新的ArrayList();
@模式(description=“此商店的所有排队\“购物\”用户状态列表,也称为购物者”)
@OneToMany(mappedBy=“商店”,
cascade=CascadeType.PERSIST,
孤立删除=真)//拥有方
@其中(子句=“用户状态=‘购物’和活动”)
@杰索尼奥雷
私有列表购物者=新的ArrayList();
//其他一些存储实例变量、方法和信息
}
@实体
@表(name=“user\u state”)
公共类**UserStatus**实现可比较的{//**子类**
@身份证
@生成值
@列(name=“id”)
私有int-id;
@列(name=“user_state”,columnDefinition=“enum('IDLE','WAITING','SHOPPING'))
私有字符串状态;
@列(name=“时间戳”)
私有时间戳日期时间;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“user\u id”,nullable=false)//**双向子-父关系**
私人帐户用户;
@许多酮
@JoinColumn(name=“store\u id”,nullable=false)//反向端(store是拥有方)//**双向子-父关系**
私人店铺;
@模式(description=“指示此状态当前是否处于活动状态,也称为历史记录”)
@杰索尼奥雷
私有布尔活动;
//其他方法。。。
}
这些方法之间的关系应如下所示: 商店有一个帐户队列,表示等待进入商店的人。一旦帐户首先进入队列,它就可以进入商店,这意味着离开队列进入商店,即从Store.queue中删除并添加到Store.购物者。一旦帐户离开商店,它将从购物者列表中删除。在整个过程中,我希望记录帐户执行这些操作的日期/时间。我创建了一个实体(子)类UserStatus来跟踪它。UserStatus有自己的表和行id(PK)。UserStatus.datetime是操作完成的时间,UserStatus.state是该操作的状态(“等待”如果帐户添加到商店队列,“购物”一旦帐户进入商店,“空闲”帐户离开存储后。UserStatus.user是父帐户类的主键,UserStatus.Store是父存储类的主键。我还希望帐户具有其所在位置的历史记录。这就是为什么我创建了布尔UserStatus.active以显示一旦不再需要某个操作,就应将其添加到Account.history.T帐户还应该知道其当前用户状态,即Account.status

当我将帐户添加到Store.queue时,Hibernate不会给我任何问题。我在Store中创建一个新的UserStatus,将其添加到队列中,然后返回它,这样我就可以保留UserStatus。我遇到的问题是,当一个帐户想要离开队列(而不是进入商店)时,我该怎么办?应该从商店队列中删除一个帐户,并且之前的“等待”用户状态应该从“active=true”更改为“active=false”,然后用户状态应该添加到Account.history,然后Account.status应该设置为一个新的用户状态,即“IDLE”和“active=true”。因此,实际上我应该保留两个用户状态。第一个需要更新,第二个需要添加。我在哪里以及如何执行此操作?我是否坚持使用子类、父类或所有三个类?我感谢您的帮助


PS:当从数据库中删除存储时,我希望包含该存储ID的所有UserStatus行也被删除,无论它是否处于活动状态。当从数据库中删除帐户时,我希望包含该帐户ID的所有UserStatus行也从数据库中删除。

欢迎使用StackOverflow!请尽量保持您的问题焦点:位于结束应该成为一个专门的问题。我认为你应该删除你的最后一段,并在一个新问题中发布所有必要的信息。