无需第三个表即可进行多对多的休眠 [英] Hibernate Many to Many without third table

查看:78
本文介绍了无需第三个表即可进行多对多的休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表(Users和UserRole)

I have two tables (Users and UserRole)

create table Users (
    UserId serial not null,
    UserName varchar(100) not null,
    UserType varchar(15) not null,
    Constraint PK_Users primary key (UserId)
)
;

CREATE TABLE UserRole
(
   RoleId serial not null, 
   RoleType varchar(20) not null, 
   AccessTo varchar(100) not null,  
   CONSTRAINT PK_UserRoleId PRIMARY KEY (RoleId)
) 
;

insert into Users (default,'Raj','Admin');
insert into Users (default,'Kumar','Internal');
insert into Users (default,'Ramesh','Internal');
insert into Users (default,'Muthu','External');
insert into Users (default,'Sundar','External');

insert into UserRole (default, 'Admin','/**');
insert into UserRole (default, 'Internal','/parking/*');
insert into UserRole (default, 'Internal','/vehciles/*');
insert into UserRole (default, 'External','/Upload/*');
insert into UserRole (default, 'External','/ViewParkings/*');

//以下联接语法不起作用

// The following join syntax doesn't work

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "UserRole",
JoinColumns(
{
    JoinColumn(updatable=false,insertable=false, name="UserType"
             , referencedColumnName="RoleType"),

}
private Set<UserRole> userRoles = new HashSet<userRoles>();

我正在尝试在两个表之间建立连接而不使用第三个表(链接表).我可以使用休眠模式吗?

I am trying to establish connection between two tables without using third table (link table). Is it possible when I use hibernate?

推荐答案

对于多对多关系,您必须拥有一个连接表以及来自其他两个表的键,因此工作原理并非如此. 休眠注释只是现有ERD的映射

It doesn't work like that for a many to many relationship you must have a join table with the keys from the other two tables. hibernate annotaion is just a mapping for the existing ERD

这是文档

打开声明javax.persistence.ManyToMany

Open Declaration javax.persistence.ManyToMany

@Target(value = {METHOD,FIELD}) @Retention(value = RUNTIME)

@Target(value={METHOD, FIELD}) @Retention(value=RUNTIME)

定义具有多对多多重性的多值关联.

Defines a many-valued association with many-to-many multiplicity.

每个多对多关联具有两个方面,即拥有方和非拥有方或逆向方.连接表是在拥有方上指定的.如果关联是双向的,则任何一方都可以指定为拥有方.如果关系是双向的,则非拥有方必须使用ManyToMany批注中的mappingBy元素来指定拥有方的关系字段或属性.

Every many-to-many association has two sides, the owning side and the non-owning, or inverse, side. The join table is specified on the owning side. If the association is bidirectional, either side may be designated as the owning side. If the relationship is bidirectional, the non-owning side must use the mappedBy element of the ManyToMany annotation to specify the relationship field or property of the owning side.

关系的联接表(如果未默认设置)在拥有方上指定.

The join table for the relationship, if not defaulted, is specified on the owning side.

ManyToMany批注可用于实体类中包含的可嵌入类中,以指定与实体集合的关系.如果关系是双向的,并且包含可嵌入类的实体是该关系的所有者,则非拥有方必须使用ManyToMany批注的maptedBy元素来指定可嵌入类的关系字段或属性.必须在mappingBy元素中使用点(.")表示法语法,以指示嵌入属性内的关系属性.与点符号一起使用的每个标识符的值是相应的嵌入字段或属性的名称.

The ManyToMany annotation may be used within an embeddable class contained within an entity class to specify a relationship to a collection of entities. If the relationship is bidirectional and the entity containing the embeddable class is the owner of the relationship, the non-owning side must use the mappedBy element of the ManyToMany annotation to specify the relationship field or property of the embeddable class. The dot (".") notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.

Example 1:

// In Customer class:

@ManyToMany
@JoinTable(name="CUST_PHONES")
public Set<PhoneNumber> getPhones() { return phones; }

// In PhoneNumber class:

@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }

Example 2:

// In Customer class:

@ManyToMany(targetEntity=com.acme.PhoneNumber.class)
public Set getPhones() { return phones; }

// In PhoneNumber class:

@ManyToMany(targetEntity=com.acme.Customer.class, mappedBy="phones")
public Set getCustomers() { return customers; }

Example 3:

// In Customer class:

@ManyToMany
@JoinTable(name="CUST_PHONE",
    joinColumns=
        @JoinColumn(name="CUST_ID", referencedColumnName="ID"),
    inverseJoinColumns=
        @JoinColumn(name="PHONE_ID", referencedColumnName="ID")
    )
public Set<PhoneNumber> getPhones() { return phones; }

// In PhoneNumberClass:

@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }

因为: Java Persistence 1.0

Since: Java Persistence 1.0

这篇关于无需第三个表即可进行多对多的休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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