简体   繁体   English

实现Spring数据JPA中的问题

[英]Issue in Implementing Spring data JPA

I am new to JPA and I am facing issue in creating a scenario. 我是JPA的新手,我在创建场景时遇到了问题。
I have following 3 tables. 我有以下3个表。

"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. 我有一个场景,例如,当现有用户注册某个角色的可用事件时,应将UserId,事件ID,角色ID插入“Event_Member表”。
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. 我使用4th作为Mapping表在前3个表之间创建了多对多映射,但在大多数代码示例中,给出了当数据持久保存到User,Event,Role表时,它将在Event中创建一个条目 - 成员表本身。
But in my case I dont need to Insert in the first 3 tables as it will contain data already. 但在我的情况下,我不需要插入前3个表,因为它已经包含数据。 I just want to fetch from the User,Event,Role table and put in the 4th table. 我只想从User,Event,Role表中获取并放入第4个表。 I will get the data using JSON(REST service). 我将使用JSON(REST服务)获取数据。 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. Edit1:我们可以在JPA中使用继承策略吗?

    @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. 在输入JSON中,只有用户电子邮件,Event_Name,Role-Name将在JSON中给出。 All these are unique key in those tables. 所有这些都是这些表中的唯一键。

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) 1.对于所有连接列,您不需要@ID 2.应更正为@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 在数据库级别“event-member”表的“UserID”应该是“user”表的外键

follow accordingly for Event and EventRole 相应地遵循Event和EventRole

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM