Need clarification about mapping objects to database, annotations, and one to many relationships

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?

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 {

    private Job job;

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.


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.

Check this article out:

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

You need something like this:

class Job {


    @JoinColumn (name="authorization_id", nullable = false, updatable = false, insertable = false)
    private Authorization authorization;

    @JoinColumn (name="filter_id", nullable = false, updatable = false, insertable = false)
    private Filter filter;

and on your Filter

class Filter {

      //some stuff, but no relation!

EDIT: 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:

class Authorization{
 private Long authId;

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

"foo_id" and will reference the

"auth_id" field in the

"foo" table.

TO your final comment:

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

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

@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


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

Actually, you should use many-to-one relationship, from Job to Filter and another one from Job to Authorization.

As stated in hibernate documentation (see 24.1. A note about collections), a one-to-many reference from Filter to Job will make the Job considered as part of the Filter entity, which is exactly the opposite of what you want. So, Job has to point Filter. Since many Jobs may point the same Filter, it is a many-to-one relationship.

You'd write your Job class this way:

public class Job {


    private Filter filter;

    private Authorization authorization;


Need Your Help

Is Elixir's Module.register_attribute mutability?

functional-programming immutability elixir mutability

Is it a way to create mutable state with modules? How can using this be a good idea? Wouldn't that kind of break the immutability idea from functional programming?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.