休眠仅保存具有复合主键中的父外键的子表条目
Posted
技术标签:
【中文标题】休眠仅保存具有复合主键中的父外键的子表条目【英文标题】:Hibernate Save only Child table entry with parent foreign Key in Composite primary key 【发布时间】:2014-10-31 16:43:31 【问题描述】:我是 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 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
投票.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
Vote.java 通过@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
控制器 sn-p 用于添加事件和添加投票
@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);
DAO 实现:
@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)的每个事件的唯一条目。 请为此模型建议正确的映射。 也欢迎任何改进建议。
【问题讨论】:
【参考方案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
只要确保您的交易正确无误。这只是一个想法应该如何做。
【讨论】:
以上是关于休眠仅保存具有复合主键中的父外键的子表条目的主要内容,如果未能解决你的问题,请参考以下文章