I am new to JPA and I am facing issue in creating a scenario.
I have following 3 tables.
"User" has UserId and other user Details. A Row in this is created when the User registers. "Event" has EventId and other Event Details. A Row is created when a new Event is created. "Event_role_master" table has Role ID and other columns. Has Pre-loaded set of Roles. "Event-member" table has UserID,EventId,RoleId and TimeStamp.
I have a scenario like, when a Existing users registers to an available Event for a certain role, the UserId,Event ID,Role ID should be inserted in the "Event_Member table.
I have created a Many-to-Many mapping between the first 3 tables using 4th as the Mapping table, but in most code examples it is given that when data is persisted to User,Event,Role table it will create an entry in the Event-member table by itself.
But in my case I dont need to Insert in the first 3 tables as it will contain data already. I just want to fetch from the User,Event,Role table and put in the 4th table. I will get the data using JSON(REST service). Can some please help or show some code example of how to achieve this scenario. Thanks in Advance.
Edit1: Can we use the Inheritance Strategy in JPA for this.
@Entity @Table(name="event_organiser_role") public class EventRole { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String role; private Timestamp created_at; private Timestamp updated_at; private String created_by; private String updated_by; private boolean is_deleted; @OneToOne(cascade=CascadeType.ALL) private PointMaster point; @OneToMany(mappedBy="role",cascade=CascadeType.ALL,orphanRemoval=false) private Set<EventMember> eventMember; @Entity @Table(name="user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; private String mobile; private int points; private Timestamp created_at; private Timestamp updated_at; private String created_by; private String updated_by; private boolean is_deleted; @Transient private Event event; @Transient private EventRole eventrole; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) private Set<UserBusiness> userBusiness; @OneToOne(mappedBy = "user", cascade = CascadeType.ALL) private UserAccount userAccount; @JsonIgnore @OneToMany(mappedBy="user",cascade=CascadeType.ALL,orphanRemoval=false) private Set<EventMember> eventMember; @Entity @Table(name="event_member") public class EventMember implements Serializable{ private static final long serialVersionUID = 3344138873871956378L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private Timestamp created_at; private Timestamp updated_at; private String created_by; private String updated_by; private boolean is_deleted; @Id @ManyToOne @JoinColumn(name="event") private Event event; @Id @ManyToOne @JoinColumn(name="user") private User user; @Id @ManyToOne @JoinColumn(name="event_organiser_role") private EventRole role;
Method To Insert Data (Not Working ) :
@Override public void registerEvent(User user){ EventMember member=new EventMember(); Event event=eventDAO.getEventByName(user.getEvent().getName()); EventRole role=eventDAO.getRoleByName(user.getEventrole().getRole()); user=(User) getUserbyValue(user.getEmail()); member.setEvent(event); member.setRole(role);member.setUser(user); System.out.println(member); userDAO.addUser(user); eventDAO.createEvent(event); eventSer.createEventRole(role); //userDAO.registerEvent(member); }
In the input JSON, only the User-email,Event_Name,Role-Name will be given in the JSON. All these are unique key in those tables.
1 Answers
Answers 1
I think following is not correct.
@Id @ManyToOne @JoinColumn(name="user") private User user
1.You don't need @ID for all joining columns 2.should corrected as @JoinColumn(name="UserID)
@ManyToOne @JoinColumn(name="UserID") private User user
In database level "UserID" of "event-member" table should be a foreign key of "user" table
follow accordingly for Event and EventRole
0 comments:
Post a Comment