对于使用Jersey的JAX-RS,在hibernate资源类中使用连接表的多对一

Ali*_*Ali 6 rest hibernate hql jersey

我正在使用Jersey实现RESTful Web服务.我使用hibernate与数据库(mySQL)进行通信.我的hibernate资源类包括:

@Entity
public class Activity {

    @Id
    @GeneratedValue
    private long id;

@ManyToOne
    @JoinTable(name="category_activity",
    joinColumns={@JoinColumn(name="activities_id")},
    inverseJoinColumns={@JoinColumn(name="Category_id")})
    private Category category;
}
Run Code Online (Sandbox Code Playgroud)

和类别:

@Entity
public class Category {

    @Id
    @GeneratedValue
    private long id;

    @OneToMany
    @Fetch(FetchMode.JOIN)
    @JoinTable(name = "category_activity",
    joinColumns = { @JoinColumn(name = "Category_id") }, 
    inverseJoinColumns = { @JoinColumn(name = "activities_id") })
    @JsonIgnore
    private Collection<Activity> activities;
}
Run Code Online (Sandbox Code Playgroud)

我用这个查询来获取活动:

session.createQuery("from Activity a join a.category cs where cs.id= :categoryId order by a.key").setLong("categoryId", categoryId).list();
Run Code Online (Sandbox Code Playgroud)

JSON格式的结果不正确:

[[{"id":26,"key":"other","name":"Other","cost":100.0,"category":{"id":10,"name":"General","description":""}},{"id":10,"name":"General","description":""}]]
Run Code Online (Sandbox Code Playgroud)

如你所见,类别被打印2次,我们周围有一个额外的[].当我在Category类中使用One-To-Many关系的另一种机制时,如:

@OneToMany(targetEntity = Activity.class, mappedBy = "category", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
private Collection<Project> activities;
Run Code Online (Sandbox Code Playgroud)

在Activity类中:

@ManyToOne(optional = false)
    private Category category;
Run Code Online (Sandbox Code Playgroud)

而这个查询:

session.createQuery("from Activity as a where a.category.id= :categoryId order by a.key").setLong("categoryId", categoryId).list();
Run Code Online (Sandbox Code Playgroud)

一切正常.但我必须使用连接表,因为我不想更改数据库.

正确的结果应如下所示:

[{"id":26,"key":"other","name":"Other","cost":100.0,"category":{"id":10,"name":"General","description":""}}]
Run Code Online (Sandbox Code Playgroud)

我感谢任何帮助.

JB *_*zet 15

在多方面定义连接表,但不要在一侧再次定义它.这将创建两个与同一个表映射的单向关联,而不是一个双向关联.

双向关联始终具有所有者端(您指定要使用的连接列或连接表,以及使用mappedBy属性表示它是另一侧的反转的反面):

public class Activity {

    @ManyToOne // owner side: it doesn't have mappedBy, and can decide how the association is mapped: with a join table
    @JoinTable(name="category_activity",
               joinColumns={@JoinColumn(name="activities_id")},
               inverseJoinColumns={@JoinColumn(name="Category_id")})
    private Category category;
}

public class Category {
    @OneToMany(mappedBy = "category") // inverse side: it has a mappedBy attribute, and can't decide how the association is mapped, since the other side already decided it.
    @Fetch(FetchMode.JOIN)
    @JsonIgnore
    private Collection<Activity> activities;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

此外,您的查询应该只选择活动,而不是通过添加select子句来选择查询加入的所有实体:

select a from Activity as a where a.category.id= :categoryId order by a.key
Run Code Online (Sandbox Code Playgroud)