用额外的列映射多对多关联表 [英] Mapping many-to-many association table with extra column(s)

查看:22
本文介绍了用额外的列映射多对多关联表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库包含 3 个表:User 和 Service 实体具有多对多关系,并与 SERVICE_USER 表连接如下:

My database contains 3 tables: User and Service entities have many-to-many relationship and are joined with the SERVICE_USER table as follows:

用户 - SERVICE_USER - 服务

USERS - SERVICE_USER - SERVICES

SERVICE_USER 表包含额外的 BLOCKED 列.

SERVICE_USER table contains additional BLOCKED column.

执行此类映射的最佳方法是什么?这些是我的实体类

What is the best way to perform such a mapping? These are my Entity classes

@Entity
@Table(name = "USERS")
public class User implements java.io.Serializable {

private String userid;
private String email;

@Id
@Column(name = "USERID", unique = true, nullable = false,)
public String getUserid() {
return this.userid;
}

.... some get/set methods
}

@Entity
@Table(name = "SERVICES")
public class CmsService implements java.io.Serializable {
private String serviceCode;

@Id
@Column(name = "SERVICE_CODE", unique = true, nullable = false, length = 100)
public String getServiceCode() {
return this.serviceCode;
}
.... some additional fields and get/set methods
}

我遵循了这个例子http://giannigar.wordpress.com/2009/09/04/m ...使用-jpa/下面是一些测试代码:

I followed this example http://giannigar.wordpress.com/2009/09/04/m ... using-jpa/ Here is some test code:

User user = new User();
user.setEmail("e2");
user.setUserid("ui2");
user.setPassword("p2");

CmsService service= new CmsService("cd2","name2");

List<UserService> userServiceList = new ArrayList<UserService>();

UserService userService = new UserService();
userService.setService(service);
userService.setUser(user);
userService.setBlocked(true);
service.getUserServices().add(userService);

userDAO.save(user);

问题是hibernate持久化了User对象和UserService一个.CmsService 对象没有成功

The problem is that hibernate persists User object and UserService one. No success with the CmsService object

我尝试使用 EAGER fetch - 没有进展

I tried to use EAGER fetch - no progress

是否可以通过上面提供的映射实现我期望的行为?

Is it possible to achieve the behaviour I'm expecting with the mapping provided above?

也许有一些更优雅的方式来映射具有附加列的多对多连接表?

Maybe there is some more elegant way of mapping many to many join table with additional column?

推荐答案

由于 SERVICE_USER 表不是纯连接表,而是有额外的功能字段(阻塞的),你必须将它映射为一个实体,并将许多分解为用户和服务之间的多个关联分为两个 OneToMany 关联:一个 User 有多个 UserService,一个 Service 有多个 UserService.

Since the SERVICE_USER table is not a pure join table, but has additional functional fields (blocked), you must map it as an entity, and decompose the many to many association between User and Service into two OneToMany associations : One User has many UserServices, and one Service has many UserServices.

您没有向我们展示最重要的部分:实体之间关系的映射和初始化(即您遇到问题的部分).所以我会告诉你它应该是什么样子.

You haven't shown us the most important part : the mapping and initialization of the relationships between your entities (i.e. the part you have problems with). So I'll show you how it should look like.

如果你使关系双向,你应该有

If you make the relationships bidirectional, you should thus have

class User {
    @OneToMany(mappedBy = "user")
    private Set<UserService> userServices = new HashSet<UserService>();
}

class UserService {
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "service_code")
    private Service service;

    @Column(name = "blocked")
    private boolean blocked;
}

class Service {
    @OneToMany(mappedBy = "service")
    private Set<UserService> userServices = new HashSet<UserService>();
}

如果你没有在你的关系上放置任何级联,那么你必须坚持/保存所有实体.虽然只有关系的拥有方(这里是 UserService 方)必须被初始化,但确保双方保持一致也是一个很好的做法.

If you don't put any cascade on your relationships, then you must persist/save all the entities. Although only the owning side of the relationship (here, the UserService side) must be initialized, it's also a good practice to make sure both sides are in coherence.

User user = new User();
Service service = new Service();
UserService userService = new UserService();

user.addUserService(userService);
userService.setUser(user);

service.addUserService(userService);
userService.setService(service);

session.save(user);
session.save(service);
session.save(userService);

这篇关于用额外的列映射多对多关联表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆