【问题标题】:How to get data with @ManyToMany annotated tables?如何使用 @ManyToMany 注释表获取数据?
【发布时间】:2016-06-06 15:21:20
【问题描述】:

我的相关表格如下。 人员表

  1. 身份证
  2. 姓名

位置表

  1. 身份证
  2. 标题

Person_Location 表

  1. person_id
  2. location_id

我想得到这样的人和位置值..


标识 |姓名 |姓氏 |标题 |点

1 约翰 |阿达斯 |我的家| 44,45
1 |约翰 |阿达斯 |兄弟之家| 55,33

如何在休眠状态下获取用户及其位置?

【问题讨论】:

  • 在您的情况下@ManyToMany 表示一个人有很多位置,并且一个位置属于很多人,因此您尝试获得的结果与您所做的映射不兼容。对于每个人,您将有一个位置列表,这意味着标题列表和点列表
  • 位置就像在swarm中。一个人有很多位置,一个位置有很多人。
  • 是的,这就是我之前告诉你的,而且你会为每个人获得不止一个位置。啊,现在我知道你想让这个人显示多少位置?
  • 是的,就像你说的那样。许多人可以是 swarm 中的一个位置。一个人可以是不同日期的很多位置。(foursquare)
  • 其实我需要通过“gezi_gezgin”表(Join Table)中的personId和locationId来获取两个表的记录。

标签: sql hibernate spring-mvc many-to-many


【解决方案1】:

试试这个:

你可以得到一个对象数组,你可以随意操作它们

String stringQuery="select p.id,p.name,p.surname,l.title,l.point from person p, location l,person_location pl where "
                + "p.id=pl.person_id and l.id=pl.location_id";
        Query query=entityManager.createNativeQuery(stringQuery);
        List<Object[]> result=query.getResultList();

稍后您可以通过result.get(i)[0]获取人员ID

或者您可以创建一个不是托管实体的自定义类:

public class customPerson{
id | name | surname | title | point
private int id;
private String name;
private String surname;
private String title;
private String doube point;

//getters&setters

//constructors (required) one default ant one with all your attributes
public CustomPerson(){}
public customPerson(int id,...){
...
}

}

稍后在你的 Dao 中你可以通过自定义对象得到你想要的结果:

String stringQuery="select p.id,p.name,p.surname,l.title,l.point from person p, location l,person_location pl where "
                + "p.id=pl.person_id and l.id=pl.location_id";
        Query query=entityManager.createNativeQuery(stringQuery,CustomPerson.class);
        List<CustomPerson> result=query.getResultList();

【讨论】:

    【解决方案2】:

    // 在 Person 类中:

        @ManyToMany
        @JoinTable(name="Person_Location",
            joinColumns=
                @JoinColumn(name="person_id", referencedColumnName="ID"),
            inverseJoinColumns=
                @JoinColumn(name="location_id", referencedColumnName="ID")
            )
        public Set<Locations> getLocations() { return locations; }
    

    // 在位置:

        @ManyToMany(mappedBy="locations")
        public Set<Person> getPersons() { return persons; }
    

    【讨论】:

    • 我已经有了你写的那些字段和注释。我的意思是我想使用 Person_Locations 表获取 Person 和 Location 表的记录。
    【解决方案3】:

    在个人类中

    @OneToMany(mappedBy="person")
        public Set<personLocations> getPersonLocation { return personLocations; }
    

    在位置类中

    @OneToMany(mappedBy="location")
        public Set<personLocations> getPersonLocation { return personLocations; }
    

    个人位置

     @ManyToOne
        @JoinColumn(name="person_ID")
        public Person getPerson() { return person; }
    
     @ManyToOne
        @JoinColumn(name="location_ID")
        public Location getlocation() { return location; }
    

    【讨论】:

    • 我在我的 person 类中使用了@ManyToMany 注释,如下所示。正如我所说,位置就像在群中。一个人有很多位置,一个位置有很多人。 paste.ubuntu.com/17068410
    • location 表和 person 表之间的映射可以是多对多的,但根据我对 location_person 表中列出的列的理解,locationPerson 和 person 之间的映射只会是多对一对多
    猜你喜欢
    • 2012-02-14
    • 2021-04-16
    • 1970-01-01
    • 2016-02-03
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多