需要澄清对象映射到数据库,注释和一对多的关系 [英] Need clarification about mapping objects to database, annotations, and one to many relationships

查看:225
本文介绍了需要澄清对象映射到数据库,注释和一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些帮助理解,如果我在这里在正确的轨道上,并正是我需要做的。希望有人能附和

Need some help understanding if I'm on the right track here and exactly what I need to do. Hope someone can chime in.

所以,我有三个表,工作,授权和过滤。每个作业必须有一个授权和一个过滤器。当然,每个过滤器可以由一个以上的作业可以使用,并且同样适用于授权。然而,过滤器和授权并不需要知道他们正在使用的什么工作。

So I've got three tables, Job, Authorization, and Filter. Each Job must have one Authorization and one Filter. Of course, each Filter could be used by more than one job and the same goes for the Authorization. However, the Filters and Authorizations don't need to know what Jobs they are being used by.

所以这是一个对过滤器和授权的一部分,我相信很多正确的关系?还是我偏离了轨道?这是怎么仿照数据库?我相信工作表需要有一个外键字段筛选和授权表。执行滤镜和授权表需要外键的字段自己的工作吗?

So that is a one to many relationship on the part of the Filters and Authorizations I believe right? Or am I off track? How is this modeled in a database? I believe the Job table needs to have a foreign key field to the Filter and Authorization tables. Do the Filter and Authorization tables need foreign key fields themselves to Job?

接下来,我该如何在Java对象建模这一点,那我怎么设置休眠或Java持久性注解?我相信Filter类的样子:

Next, how do I model this in Java objects and then how do I set up the hibernate or java persistence annotations? I believe the Filter class looks like:

class Filter {

    @OnetoMany
    @JoinColumn(name="filterID")
    private Job job;
}

和授权类基本相同,然后将作业类会怎么看?这真的是我扔的事情是,过滤器和授权类不需要什么工作他们正在与相关联的,所以我不认为有必要为他们实际持有到作业对象的引用任何知识。只有Job类需要坚持的筛选和授权对象的引用。所以,我真的需要上述code?

and the Authorization class basically the same and then how would the Job class look? The thing that's really throwing me is that the Filter and Authorization classes don't need any knowledge of what Job they're associated with so I don't see the need for them to actually hold references to Job objects. Only the Job class needs to hold references to the Filter and Authorization objects. So do I really need the above code?

我希望有人能帮助澄清这一切对我来说,我似乎无法环绕它我的头。数据库是不是我的强项。谢谢你。

I'm hoping someone can help clarify all this for me as I can't seem to wrap my head around it. Databases are not my strong suit. Thanks.

推荐答案

据我所知,使用@OneToMany批注不是很在您的情况建议。也许你可能希望对方给自己的关系。

As far as I know, using a @OneToMany annotation is not very recommended in your situation. Maybe you may want the other side to own the relation.

检查这篇文章出来:
http://josian.word$p$pss.com/2006/09/09/hibernate-annotations-bidirectional-one-to-many/

所以,你的问题,不,你并不需要上述code。

So, to your question, no, you do not need the above code.

您需要的是这样的:

class Job {

    @Id
    //stuff...


    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn (name="authorization_id", nullable = false, updatable = false, insertable = false)
    private Authorization authorization;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn (name="filter_id", nullable = false, updatable = false, insertable = false)
    private Filter filter;
}

和你的过滤器

@Entity
@Table(name="filter")
class Filter {

      //some stuff, but no relation!
}

编辑:
该@JoinColumn就是要出现在工作表的名称。默认情况下,如果你不指定连接列名,这将是{} foreignTableName _id。在Authorizatrion的情况下,如果注明是这样的:

The @JoinColumn is just the name you want to appear in your Job table. By default, if you dont specify the join çolumn name, it is going to be {foreignTableName}_id. In the case of Authorizatrion, if you annotated it like this:

@Entity
@Table(name="foo")
class Authorization{
 @Id
 @column(name="auth_id")
 private Long authId;
}

在工作​​表中为您生成的

默认连接列将

the default join column generated for you in the Job table will be

foo_id,并引用

auth_id在字段中的

foo的表。

你最后的评论:

其实你的不需要以把外键的分贝。在作业的实体,在

Actually you don't need to put foreign keys in the db. In the Job Entity, the

@ManyToOne批注已经确保了键将你放置。在

@ManyToOne annotation already ensures the keys will be placed for you. The

@JoinColumn 指定外键的名称。例如,如果你想在工作表中的授权外键的名字被称为authorization_fk',你可以使用

@JoinColumn specifies the names of the foreign key. For example, if you want your foreign key name in the Job table to Authorization to be called 'authorization_fk', you would use

@JoinColumn('authorization_fk')

这是它是如何会被放置在您的工作表中。

and this is how it is gonna be placed in your Job table.

这篇关于需要澄清对象映射到数据库,注释和一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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