简体   繁体   English

从3个表定义休眠实体

[英]Define hibernate entity from 3 tables

I'm working on a legacy application and I was asked to integrate Hibernate (this is my first time working with it). 我正在使用旧版应用程序,因此有人要求我集成Hibernate(这是我第一次使用它)。 This app has 3 tables (among others) as follows: 这个程序有3个表格(以及其他),如下所示:

Table SITE             Table PARAMS        Table TRANS
==========             ==============      ===============
pk: id (INT)           pk: p_id (INT)      pk: t_id (INT)
    lang_id (INT)      name (CHAR)         fk: p_id (INT)
                       value (INT)         fk: lang_id (INT)
                                           text (CHAR)

Then I need to get the parameters from PARAMS along with their translations, stored in TRANS, according to the language defined for the application, which is stored in SITE. 然后,我需要根据为应用程序定义的语言(存储在SITE中)从PARAMS中获取参数及其转换,并存储在TRANS中。

I've been struggling trying to understand how to make a join of the tables to get the data for the entity. 我一直在努力了解如何建立表的联接以获取实体的数据。 I tried using @JoinTable , but I couldn't figure out how to make a 3-way join, so I started trying with @SecondaryTables without luck. 我尝试使用@JoinTable ,但是我不知道如何进行三向@JoinTable ,因此我开始尝试@SecondaryTables没有运气。

I defined this entity to map the requested data (I know this won't work as it is now) and I'm trying to figure out the proper way to make the join. 我定义了这个实体来映射请求的数据(我知道这将无法正常工作),并且我试图找出进行联接的正确方法。

@Entity
@Table(name = "params")
@SecondaryTables(
{
    @SecondaryTable(name = "trans", pkJoinColumns = @PrimaryKeyJoinColumn(name = "p_id")),
})
public class Tparam implements Serializable
{
    @Id
    @GeneratedValue
    @Column(name = "p_id")
    private int id;

    @Column(name = "name")
    private String Name;

    @Column(name = "text")
    private String visibleText

    ...
}

Any help is appreciated! 任何帮助表示赞赏!


For reference, this SQL query gives me what I want: 作为参考,此SQL查询为我提供了我想要的:

SELECT * FROM params, lang, site WHERE params.p_id = lang.p_id AND lang.lang_id = site.lang_id;

You need to define 3 entities : 您需要定义3个实体:

1-Param 2-Lang 3-Site 1-Param 2-Lang 3-Site

@Entity
@Table(name = "params")
public class Tparam implements Serializable
{
    @Id
    @GeneratedValue
    @Column(name = "p_id")
    private int id;

    @Column(name = "name")
    private String Name;

    @Column(name = "text")
    private String visibleText

    ...
}

@Entity
@Table(name = "lang")
public class Lang implements Serializable
{
    @Id
    @GeneratedValue
    @Column(name = "lang_id")
    private int id;

    @ManyToOne
    @JoinColumn(name = "p_id")
    private Tparam param;

    ...
}

@Entity
@Table(name = "site")
public class Site implements Serializable
{
    @Id
    @GeneratedValue
    @Column(name = "site_id")
    private int id;


    @ManyToOne
    @JoinColumn(name = "lang_id")
    private Lang lang

    ...
}

Then, when you need the data you can use Criteria (or Query) on Site entity to fetch data. 然后,当您需要数据时,可以使用“ Site实体上的条件(或查询)来获取数据。 Each record of site contains one Lang and each of them contains one Tparam . 每个站点记录包含一个Lang ,每个记录包含一个Tparam

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM