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 {

    @OnetoMany
    @JoinColumn(name="filterID")
    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.

Answers


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: http://josian.wordpress.com/2006/09/09/hibernate-annotations-bidirectional-one-to-many/

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

You need something like this:

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;
}

and on your Filter

@Entity
@Table(name="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:

@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" 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

@JoinColumn('authorization_fk')

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 {

    ...

    @ManyToOne
    @JoinColumn(name="filter_fk")
    private Filter filter;

    @ManyToOne
    @JoinColumn(name="authorization_fk")
    private Authorization authorization;

}

Need Your Help

Implementation of two legged OAuth 2.0 for WebApi

asp.net-mvc-4 oauth-2.0 asp.net-web-api two-legged

I created a mvc4 webapi project using VS2012RC. I tried to implement two legged Oauth 2 in my project. I followed the tutorial

If I make a 100% OpenGL-based user interface in Android or iOS, is it possible to embed a Google Map widget in it?

android ios android-layout opengl-es android-widget

I've never done such a thing before. The reason I ask is because I'm contemplating on making my own user interface using OpenGL so that it is cross platform on Android, iOS, etc. By doing so I will

Error when get HTML from web in android

android url android-emulator avd android-version

I have 1 EditText 1Button and 1TextView, when I type url in Edittext and click button, the textView will be show the Html from website that i type the url in edittext. I want to get html from web by

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.