java – 实现Spring数据JPA中的问题

我是JPA的新手,我在创建场景时遇到了问题.

我有以下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.

我有一个场景,例如,当现有用户注册某个角色的可用事件时,应将UserId,事件ID,角色ID插入“Event_Member表”.
我使用4th作为Mapping表在前3个表之间创建了多对多映射,但在大多数代码示例中,给出了当数据持久保存到User,Event,Role表时,它将在Event中创建一个条目 – 成员表本身.
但在我的情况下,我不需要插入前3个表,因为它已经包含数据.
我只想从User,Event,Role表中获取并放入第4个表.我将使用JSON(REST服务)获取数据.
有些人可以帮助或显示一些如何实现这种情况的代码示例.
提前致谢.

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;

插入数据的方法(不工作):

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

在输入JSON中,只有用户电子邮件,Event_Name,Role-Name将在JSON中给出.所有这些都是这些表中的唯一键.

最佳答案 我认为以下是不正确的.

@Id
@ManyToOne
@JoinColumn(name="user")
private User user

1.对于所有加入列,您不需要@ID
2.应更正为@JoinColumn(name =“UserID)

@ManyToOne
@JoinColumn(name="UserID")
private User user

在数据库级别“event-member”表的“UserID”应该是“user”表的外键

相应地遵循Event和EventRole

点赞