Hibernate仅保存具有复合主键中的父外键的子表条目

时间:2014-10-31 16:43:32

标签: java hibernate spring-mvc

我是Hibernate& amp; Spring MVC并努力为我的问题找到正确的解决方案。 我有父表(事件)和子表(投票)。我想在从URL接收数据时仅保存子表条目。我在映射关系和将复合键放到工作中遇到了麻烦

以下是我的结构:

create table Events(
Event_ID int(11) NOT NULL AUTO_INCREMENT,
Event_Name varchar(200) NOT NULL,
Event_options int(1) NOT NULL,
Start_TIME timestamp,
End_time   timestamp,
Active_Status int(1),
PRIMARY KEY(Event_ID)
)ENGINE=InnoDB AUTO_INCREMENT=16
;

create table Votes(
Event_ID int(11) NOT NULL,
Voter_MSISDN int(13) NOT NULL,
Vote_Option int(1),
PRIMARY KEY(Event_ID,Voter_MSISDN),
FOREIGN KEY (Event_ID) REFERENCES Events(Event_ID)
)ENGINE=InnoDB AUTO_INCREMENT=16
;

Events.java

@Entity
@Table(name="Events")
public class Events {

    @Id
    @GeneratedValue
    @Column(name = "Event_ID")
    private Integer eventId;

    @Column(name="Event_Name")
    private String eventName;

    @Column(name="Event_options")
    private Integer eventOptions;

    @Column(name="Start_TIME")
    private String startTime;

    @Column(name = "End_time")
    private String End_time;

    @Column(name="Active")
    private Integer status;

    @OneToMany(mappedBy = "Events")
    private Set<Votes> votes;
     //Setter Getters

}

Votes.java

@Entity
@Table(name="Votes")
public class Votes {

    public Votes(){}        

    @EmbeddedId
    private Vote vote;

    @Column(name = "Vote_Option")
    private int Vote_Option;


    @ManyToOne
    @JoinColumn(name = "Event_ID")
    private Events events;

    //setters getters
}

通过@Embeddable

进行复合主键设置的Vote.java
@Embeddable
public class Vote implements Serializable{

        public Vote(){}

        @Column(name="Event_ID")
        private int Event_ID;

        @Column(name="Voter_MSISDN")
        private long Voter_MSISDN;

        //setter getters
}

添加事件和添加投票的控制器代码段

@RequestMapping(value="/AddEvent")
    @ResponseStatus(value = HttpStatus.OK)
    public void AddEvent(@RequestParam(value = "ename", required = true) String ename, 
                        @RequestParam (value = "opt") String opt, 
                        @RequestParam (value = "stime") String start,
                        @RequestParam (value = "etime") String end,
                        @RequestParam (value = "status") String active){

        Events event = new Events();
        event.setEventName(ename);
        event.setEventOptions(Integer.parseInt(opt));
        event.setStartTime(start);
        event.setEnd_time(end);
        event.setStatus(Integer.parseInt(active));

        userDao.saveEvent(event);
    }

    @Autowired
    private Vote vote;

    @Autowired
    private Votes votes;

    @RequestMapping(value="/AddVote")
    @ResponseStatus(value = HttpStatus.OK)
    public void AddVote(@RequestParam(value = "eventid",required = true) String eventid,
                        @RequestParam(value="msisdn") String msisdn,
                        @RequestParam(value = "opt")String opt){

        logger.info("Received parameters from URL "+eventid+" "+msisdn+" "+opt);
        vote.setEvent_ID(Integer.parseInt(eventid));


        vote.setVoter_MSISDN(Long.parseLong(msisdn));

        votes.setVote(vote);
        votes.setVote_Option(Integer.parseInt(opt));

        userDao.saveVotes(votes);           
    }
}

DAOImplementation:

@Transactional
    public void saveEvent(Events event) {
        // TODO Auto-generated method stub
        Session session = sessionFactory.getCurrentSession();

        session.save(event);

    }

    @Transactional 
    public void saveVotes(Votes votes){

        Session session = sessionFactory.getCurrentSession();

        session.save(votes);
    }

只要收到事件数据并添加了事件条目,代码就可以正常工作。 无法正确编码投票数据。 我想在从url收到投票数据时插入。我添加了Composite主键,以确保来自一个用户(MSISDN)的每个事件的唯一条目。 请为此模型建议正确的映射。 任何改进建议也欢迎。

1 个答案:

答案 0 :(得分:1)

首先,您不需要这个简单解决方案的复合键。

你有一个班级:

@Entity
@Table(name="Events")
public class Events {

    //....................

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "Events")
    private Set<Votes> votes = new HashSet<>();

}

简单地在其中创建一个方法,该方法将为该事件创建新的投票。例如:

public Vote createVote() {
    Vote vote = new Vote();
    vote.setEvent(this);
    votes.add(vote);
    return vote;
}

然后在你的addVote控制器方法中:

@RequestMapping(value="/AddVote")
@ResponseStatus(value = HttpStatus.OK)
public void AddVote(@RequestParam(value = "eventid",required = true) String eventid,
                    @RequestParam(value="msisdn") String msisdn,
                    @RequestParam(value = "opt")String opt){

    Event event = userDao.findEvent(eventid);
    Vote vote = event.createVote(); //This will create a vote for an event.

    vote.set.... //set your stuff.
    //It will cascade your vote to an event if you have a cascade sorted correctly as in the example above: cascade = CascadeType.ALL
}

确保您的交易正确无误。这只是一个想法应该如何完成。