2014-10-31 88 views
0

我是休眠初學者& 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 
} 

投票.java for Composite主鍵通過@Embeddable設置

對於添加事件和添加投票
@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

首先,您不需要這個簡單解決方案的組合鍵。

你有一個類:

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

只要確保你有你的交易權。這只是一個想法應該如何完成。