休眠只保存复合主键中具有父外键的子表项
问题描述:
我是休眠初学者& 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)的每个事件的唯一条目。 请建议此模型的正确映射。 也欢迎任何改进建议。
答
首先,您不需要这个简单解决方案的组合键。
你有一个类:
@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
}
只要确保你有你的交易权。这只是一个想法应该如何完成。