-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to bridge relationships using JPA
PostPosted: Thu Mar 26, 2015 1:41 pm 
Newbie

Joined: Sat Jul 11, 2009 12:31 am
Posts: 5
Am new to JPA...

I have the following preexisting tables inside a MySQL 5 database:

App
User
User_App_Bridge

The relationship(s) are mapped to the User_App_Bridge table (as the name implies, a bridge) amongst App and User as a One to Many cardinality.

The E-R diagram:

User ------ User_App_Bridge------ App

1 User is associated with many apps inside the User_App_Bridge table.

1 App is associated with many users inside the User_App_Bridge table.

The DDL for the User_App_Bridge table:

Code:
CREATE TABLE `User_App_Bridge` (
   `User_App_Bridge_Id` int(11) NOT NULL AUTO_INCREMENT,
   `User_Id` int(11) NOT NULL,
   `App_Id` int(11) NOT NULL,
   PRIMARY KEY (`User_App_Bridge_Id`),
   KEY `App_Id` (`App_Id`),
   KEY `User_Id` (`User_Id`),
   CONSTRAINT `user_app_bridge_ibfk_1` FOREIGN KEY (`App_Id`) REFERENCES `App` (`App_Id`))
   ENGINE=InnoDB DEFAULT CHARSET=utf8;


Lets say that I have the following JPA Annotated Classes which are mapped to these tables:
Code:
@Entity
public class App {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "App_Id")
   private long appId;

   @OneToMany
   @JoinTable
   (
      name = "UserAppBridge",
      joinColumns = { @JoinColumn(name="App_Id", referencedColumnName = "App_Id") },
      inverseJoinColumns = { @JoinColumn(name="User_Id", referencedColumnName = "User_Id") }
   )
   private List<User> users;
      
   @Column(name = "App_Name")
   private String appName;

   // Getters & Setter methods
}

Code:
@Entity
public class User {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "User_Id")
   private long userId;

   @OneToMany
   @JoinTable
   (   
      name = "UserAppBridge",
      joinColumns = { @JoinColumn(name="User_Id", referencedColumnName = "User_Id") },
      inverseJoinColumns = { @JoinColumn(name="App_Id", referencedColumnName = "App_Id", unique = true) }
   )
   private List<App> apps;

   @Column(name = "User_Name")
   private String userName;

   // Getters & Setter methods
}

Code:
@Entity
public class UserAppBridge {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "User_App_Bridge_Id")
   private long userAppBridgeId;

   @Column(name = "User_Id")
   private long userId;

   @Column(name = "App_Id")
   private long appId;

   // Getters & Setter methods
}


Question(s):

(1) Is this (the block inside the @JoinTable) the correct way to do the one to many mapping for User and App entites with the UserAppBridge?

(2) Inside the @JoinTable should the column & referencedColumnName be assigned to the SQL value (e.g. name="User_Id" , referencedColumnName = "User_Id")
or should it be the Java reference name (e.g. name="userId" , referencedColumnName = "userId")?

(3) Inside the inverseJoinColumns code black, is the unique = true necessary (what is it for)?

(4) Do I need to do anything else inside the UserAppBridge class (for connecting to App and User)?

Thank you for taking the time to read this...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.