- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想与双方的同一个表创建ManyToMany
关系。它应该代表两个用户之间的友谊。我现在有用户类别:
@Entity
@Table(name = "Users")
public class User extends BaseObject implements UserDetails {
//example of others relationship
private Set<Post> posts;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true
, mappedBy = "author")
@OrderBy("dateOfCreation DESC")
public Set<Post> getPosts() {
return posts;
}
public void setPosts(Set<Post> posts) {
this.posts = posts;
}
}
和@MappedSuperclass
@MappedSuperclass
public class BaseObject {
/** Object id */
private Long id;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id)
{
this.id = id;
}
}
因为我想向此关系添加一些新列,所以我必须创建将用作连接表的新实体。所以我知道UserFriend
:
@Entity
@Table(name = "UserFriends")
public class UserFriend {
/** User */
private User user;
/** User's friend */
private User friend;
/** Pending friendship*/
private boolean accepted;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public User getFriend() {
return friend;
}
public void setFriend(User friend) {
this.friend = friend;
}
//other getter setter
}
现在我需要连接这些表。我想使用 user_id
和 friend_id
指向 User
实体的 id
字段。但我不确定如何实现这一目标。您能给我提示如何使用正确的注释吗?或者给我更好的方法(例如使用新字段用户)。
最佳答案
您可以使用单向 ManyToOne
映射。
/** User */
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
/** User's friend */
@ManyToOne
@JoinColumn(name = "friend_id")
private User friend;
Hibernate 生成表UserFriends
,其中包含user_id
和friend_id
列。这些列包含由映射父类(super class)的 Id
引用的已连接用户。
对 Hibernate 生成的 DDL 语句的快速验证确认了该功能。
create table UserFriends (id bigint not null, accepted boolean not null, friend_id bigint, user_id bigint, primary key (id))
create table Users (id bigint not null, primary key (id))
关于java - 具有 MappedSuperclass 字段的同一实体上的多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48233506/
在 JPA/Hibernate 中,像 BetterBaseEntity 这样的映射父类(super class)可以是另一个映射父类(super class) BaseEntity 的子类吗? 我想
我在 Symfony 5 中与 MappedSuperclass 的多对一关系存在问题。我有实体 Employee、Employer,它扩展了 MappedSuperclass 抽象类 Person。
伙计们! 如果我定义一个实体类并用 @MappedSuperclass 注释它和一个 @EntityListener ,在子类中是否还会调用监听器来处理生命周期事件? 例子: @MappedSuper
我在搜索从@MappedSuperclass 扩展的实体时遇到了问题。我创建了一个类 PhoneBook 并从中扩展了 2 个实体:FirstPhoneBook 和 SecondPhoneBook。结
我有一个映射父类(super class) @MappedSuperclass public class A{ . . . @Column(name="somethin
我想知道是否有办法在继承 MappedSuperClass 的子类中更改(或定义内部)注释,例如,假设我们有一个类 BaseUser(mappedsuperclass),一个子类 User :
我有一些表有一些公共(public)列,我认为在创建实体时我会让实体扩展一个定义了所有映射的类: @MappedSuperclass public class AbstractLogMaster im
假设我有一个 JPA 实体: @Entity @Table(name = "PARENT") public class Parent { // ... } 有没有办法,也许是特定于 Hiber
我正在努力创建一些抽象类来抽象每个实体将共享的属性,例如 ID。 我创建了一个 AbstractModel 类,如下所示。 import javax.persistence.*; import jav
我想与双方的同一个表创建ManyToMany关系。它应该代表两个用户之间的友谊。我现在有用户类别: @Entity @Table(name = "Users") public class User e
我的 Web 应用程序中有几个领域模型类,它们之间存在层次关系。其中一个例子是用于对用户帖子进行分类的分层类别结构。 有一些与这些类的层次结构相关的逻辑是常见的。因此,我尝试将逻辑移动到通用的 @Ma
问题 我有一个名为 Data 的 @MappedSuperclass 作为我数据库中每个实体的父级。它包含 Id 等公共(public)属性。然后我有一个扩展 Data 的实体,由于其子类的公共(pu
我有一个摘要TemporalModel向所有扩展模型添加 created 和 updated 字段的类(用 @MappedSuperclass 注释)。我想给它添加一个 getLatest() 静态方
我需要关联 @OneToMany 从 Country 到父类(super class) Place (@MappedSuperclass)。它可以是双向的。我需要像 @OneToAny 这样的东西。
有: @MappedSuperclass class Superclass { @Id @Column(name = "id") protected long id;
我有一个场景,我需要使用 Doctrine 的 MappedSuperclass 功能(使用 Symfony2),并在一些父类(super class)列上创建一个唯一约束。比方说: use Doct
我将 JPA2 与 Hibernate 一起使用,并尝试为我的实体引入一个公共(public)基类。到目前为止,它看起来像这样: @MappedSuperclass public abstract c
我可以在父类和子类中定义相同的 JPA 回调方法吗?如果是,我是否需要调用 super.onPrePersist();在子类的 onPrePersist() 方法中? @MappedSuperclas
我正在使用 JPA 2.0 和 EclipseLink 2.2.0。 我有一个 @MappedSuperclass,AbstractEntity,它是我所有提供 PK 和审计列的实体的基础。 我想让另
@MappedSuperclass public abstract class AbstractBaseModel{ } @MappedSuperclass public class Person e
我是一名优秀的程序员,十分优秀!