我正在尝试为一个实体类在单列中制作两个不同的表多对一映射 [英] i'm trying to make two different table manytoone mapping in single column for one entity class

查看:33
本文介绍了我正在尝试为一个实体类在单列中制作两个不同的表多对一映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始将两个多对一映射列休眠到单列

i'm new to hibernate two manytoone mapping column make to single column

我的实体类

      @ManyToOne
      @JoinColumn(name="assignedto_id", referencedColumnName="id", insertable=false, updatable=false)   
      private User assignedto;

      @ManyToOne
      @JoinColumn(name="assignedto_id", referencedColumnName="id", insertable=false, updatable=false)
      private LDAPUser assignedto1;

mysql 表列

  KEY `FKs437l1xgobeh3iucqmifj356i` (`assignedto_id`),
  CONSTRAINT `FKs437l1xgobeh3iucqmifj356i` FOREIGN KEY (`assignedto_id`) REFERENCES `tbl_master_user` (`ID`)

我想要这样的mysql表列

i want mysql table column like this

 KEY `FKs437l1xgobeh3iucqmifj356i` (`assignedto_id`),
 CONSTRAINT `FKs437l1xgobeh3iucqmifj356i` FOREIGN KEY (`assignedto_id`) REFERENCES `tbl_master_user` (`ID`)

 KEY `FKs437l1xgobeh3iucqmifj356i` (`assignedto_id`),
 CONSTRAINT `FKs437l1xgobeh3iucqmifj356i` FOREIGN KEY (`assignedto_id`) REFERENCES `tbl_ldap_master` (`ID`)

请帮我提前谢谢

推荐答案

我认为您正在寻找的内容在 关于@Any注解的Hibernate ORM部分:

I think what you are looking for is explained in the Hibernate ORM section about @Any annotation:

@Any(metaColumn = @Column(name = "userType"))
@AnyMetaDef(name = "PropertyMetaDef", metaType = "string", idType = "long",
            metaValues = {
                    @MetaValue(value = "User", targetEntity = User.class),
                    @MetaValue(value = "LDAP", targetEntity = LDAPUser.class)
            }
    )
@JoinColumn(name="assignedto_id", referencedColumnName="id", insertable=false, updatable=false)   
private Object assignedTo

但它不会创建任何外键.我什至认为不可能在同一列上为不同的表创建两个外键(这是否有意义?).

But it won't create any foreign keys. I don't even think is possible to create two foreign keys on the same column for different tables (does it even make sense?).

或者,如果两个类可以从一个共同的超类扩展,你可以使用 继承映射 以实现类似的目的.

Alternatively, if the two classes could extend from a common super class, you could use Inheritance mapping to achieve something similar.

例如:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public static class UserEntity {

    @Id
    private Long id;

    ...
}

@Entity
public class User extends UserEntity {
...
}

@Entity
public class LDAPUser extends UserEntity {
...
}

然后

      @ManyToOne
      @JoinColumn(name="assignedto_id", referencedColumnName="id", insertable=false, updatable=false)   
      private UserEntity assignedto;

这将创建三个表.表 UserEntity 将在 assignedto_id 列上有一个外键.User id 和 LDAPUser id 将对 UserEntity id 各有一个约束.基本上,接近你最初的要求.

This will create three tables though. The table UserEntity will have a foreign key on the assignedto_id column. User id and LDAPUser id will have a constraint each on UserEntity id. Basically, getting close to what you asked originally.

这篇关于我正在尝试为一个实体类在单列中制作两个不同的表多对一映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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