Java Hibernate在复合主键中保存具有父外键的唯一子表项
我是Hibernate&Spring MVC的初学者,正在努力找到问题的正确解决方案。 我有父表(事件)和子表(投票)。每当从URL接收到数据时,我想保存唯一的子表条目。我在映射关系和使用复合键时遇到问题 以下是我的结构:Java Hibernate在复合主键中保存具有父外键的唯一子表项,java,hibernate,spring-mvc,Java,Hibernate,Spring Mvc,我是Hibernate&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
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
}
通过@embeddeble为复合主键设置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);
}
}
实施:
@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接收到投票数据时,我只想插入投票数据。我添加了复合主键,以确保来自一个用户(MSISDN)的每个事件的唯一条目。
请建议此模型的正确映射。
欢迎提出任何改进建议 首先,对于这个简单的解决方案,您不需要复合密钥 你有一门课:
@Entity
@Table(name="Events")
public class Events {
//....................
@OneToMany(cascade = CascadeType.ALL, mappedBy = "Events")
private Set<Votes> votes = new HashSet<>();
}
然后在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
}
确保你的交易是正确的。这只是一个应该怎么做的想法
public Vote createVote() {
Vote vote = new Vote();
vote.setEvent(this);
votes.add(vote);
return vote;
}
@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
}