- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的模型中有3个实体(实际上超过3个,但实际上是3个实体处于这种情况)
它们都具有多对多的关系(候选人可以有可能的空缺和许多 Activity ,空缺......候选人和......事件......)。
如果我更新候选人 - 好的结果。如果我更新空缺 - 好的结果但是如果我更新事件我就有 org.hibernate.NonUniqueObjectException
当我更改 Activity 中的空缺集合时会显示。如果我评论地方,我改变事件的空缺,我没有问题(将 ONE
放在我的代码中)转到代码:模型映射:
职位空缺:
@Entity
@Table(name = "vacancy")
@XmlRootElement(name="vacancy")
public class Vacancy {
private Integer id;
private String name;
private String description;
private Date date;
private User author;
@XmlTransient
private Set<Candidate> candidates = new HashSet<Candidate>();
private Set<VacancyStatus> statusList = new HashSet<VacancyStatus>();
private Set<Skill> skills = new HashSet<Skill>();
private Set<Note> comments = new HashSet<Note>();
private Set<Event> events = new HashSet<Event>();
public Vacancy() {
super();
}
@ManyToMany(mappedBy = "vacancies", fetch = FetchType.EAGER)
public Set<Event> getEvents() {
return events;
}
public void setEvents(Set<Event> events) {
this.events = events;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToMany(mappedBy = "vacancies", fetch = FetchType.EAGER)
public Set<Candidate> getCandidates() {
return candidates;
}
public void setCandidates(Set<Candidate> candidates) {
this.candidates = candidates;
}
@Override
public boolean equals(Object obj) {
if(obj!= null && ((Vacancy)obj).getId() == id ){
return true;
}
return false;
}
@Override
public int hashCode() {
Integer id = getId();
return id != null ? id.intValue() : super.hashCode();
}
...
}
候选人:
@Entity
@Table(name = "candidate")
@XmlRootElement(name = "candidate")
public class Candidate extends Person {
@Size(min=3,max=12)
@Pattern(regexp="[0-9]*",message="phone format must be without + and - (for example: 89123353456)")
private String phone;
@Past
private Date date;
private User author;
@Size(min=4, max=100)
@URL()
private String resumeUrl;
private List<CandidateStatus> statusList = new LinkedList<CandidateStatus>();
private Set<Vacancy> vacancies= new HashSet<Vacancy>();
private Set<Skill> skills = new HashSet<Skill>();
private List<Note> comments = new LinkedList<Note>();
private Set<Event> events = new HashSet<Event>();
public Candidate() {
super();
}
@ManyToMany(mappedBy = "candidates", fetch = FetchType.EAGER)
//@LazyCollection(LazyCollectionOption.FALSE)
public Set<Event> getEvents() {
return events;
}
public void setEvents(Set<Event> events) {
this.events = events;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "candidate_vacancy", joinColumns = @JoinColumn(name = "candidate_id"), inverseJoinColumns = @JoinColumn(name = "vacancy_id"))
@XmlTransient
public Set<Vacancy> getVacancies() {
return vacancies;
}
public void setVacancies(Set<Vacancy> vacancies) {
this.vacancies = vacancies;
}
...
}
@MappedSuperclass
public abstract class Person {
@Size(min=3)
@Pattern(regexp="[a-zA-Z]*")
private String name;
@Size(min=3)
@Pattern(regexp="[a-zA-Z]*")
private String surname;
private Integer id;
public Person() {
}
public Person(String name, String surname) {
super();
this.name = name;
this.surname = surname;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
@Column (name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
//@NotEmpty
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "surname")
//@NotEmpty
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public boolean equals(Object obj) {
if(obj!=null && ((Person)obj).getId() == id ){
return true;
}
return false;
}
@Override
public int hashCode() {
Integer id = getId();
return id != null ? id.intValue() : super.hashCode();
}
}
事件:
@Entity
@Table(name = "event")
@XmlRootElement
public class Event {
private Integer id;
@Size(min=3 )
@Pattern(regexp="[a-zA-Z]*")
private String name;
@Size(min=5)
@Pattern(regexp="[a-zA-Z]*")
private String description;
private Date date;
@Future
private Date eventDate;
private User author;
private Set<Candidate> candidates;
private Set<Vacancy> vacancies;
private EventType eventType;
private EventStatus eventStatus;
public Event() {
super();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
if(obj!=null && ((Event)obj).getId() == id ){
return true;
}
return false;
}
@Override
public int hashCode() {
Integer id = getId();
return id != null ? id.intValue() : super.hashCode();
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Set<Candidate> getCandidates() {
return candidates;
}
public void setCandidates(Set<Candidate> candidates) {
this.candidates = candidates;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Set<Vacancy> getVacancies() {
return vacancies;
}
public void setVacancies(Set<Vacancy> vacancies) {
this.vacancies = vacancies;
}
}
在我的 @Controller 类中,我编写了这样的方法:
@RequestMapping(value = "/updateEvent", method = RequestMethod.POST)
public String updateEvent(Model model,
@Valid @ModelAttribute("existedEvent") Event event,
BindingResult result,
@ModelAttribute("linkedCandidates") Set<Candidate> candidates,
@ModelAttribute("linkedvacancies") Set<Vacancy> vacancies) {
if (result.hasErrors()) {
model.addAttribute("idEvent", event.getId());
return "eventDetails";
}
if (vacancies != null) {
for (Vacancy vacancy : vacancies) {
vacancy.getEvents().add(event);
}
}
if (candidates != null) {
for (Candidate candidate : candidates) {
candidate.getEvents().add(event);
}
}
event.setVacancies(vacancies);//place ONE (if comment this line - will //work)
event.setCandidates(candidates);
eventService.update(event);//error here
return "redirect:goToEventMenu";
}
更新:
@Transactional
@Service
public class EventService {
public void update(Event event) {
eventDao.update(event);
}
....
}
@Repository("eventDaoImpl")
public class EventDaoImpl extends DaoAbstract implements EventDao {
@Autowired
CandidateDao candidateDao;
@Autowired
VacancyDao vacancyDao;
@Override
public boolean update(Event event) {
Session session = sessionFactory.getCurrentSession();
if (event == null) {
return false;
}
session.update(event);
return true;
}
...
}
抱歉有很多代码/但我不知道写在这里)
更新1
堆栈跟踪:
10.09.2013 11:46:14 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ui] threw exception [Request processing failed; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.epam.hhsystem.model.vacancy.Vacancy#6]] with root cause
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.epam.hhsystem.model.vacancy.Vacancy#6]
at org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:697)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:296)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:241)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:735)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:727)
at org.hibernate.engine.spi.CascadingAction$5.cascade(CascadingAction.java:258)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:388)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:331)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209)
at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:418)
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:358)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:334)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:166)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:132)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:364)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:338)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:241)
at org.hibernate.event.internal.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireUpdate(SessionImpl.java:786)
at org.hibernate.internal.SessionImpl.update(SessionImpl.java:778)
at org.hibernate.internal.SessionImpl.update(SessionImpl.java:774)
at com.epam.hhsystem.jpa.EventDaoImpl.update(EventDaoImpl.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy38.update(Unknown Source)
at com.epam.hhsystem.services.EventService.update(EventService.java:24)
at com.epam.hhsystem.services.EventService$$FastClassByCGLIB$$3653e1b6.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at com.epam.hhsystem.services.EventService$$EnhancerByCGLIB$$8266fd26.update(<generated>)
at com.epam.hhsystem.web.controllers.EventMenuController.updateEvent(EventMenuController.java:164)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
更新2
I noticed, that I have no problem if I write change mapping Vacancy in Event:
@ManyToMany(/*cascade = CascadeType.ALL,*/ fetch = FetchType.EAGER)// I comment cascade
public Set<Vacancy> getVacancies() {
return vacancies;
}
为什么?对于对称实体候选,此映射有效:
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Set<Candidate> getCandidates() {
return candidates;
}
如果我删除初始代码变体中的事件和空缺之间的关系 - 它效果很好
如果我在初始代码变体中删除事件和候选人之间的关系 - 它不起作用
最佳答案
如果您尝试在同一 session 中保存两个具有相同 ID 的对象,则会发生这种情况。
首先,这种情况不应该发生,尝试找出发生这种情况的原因并避免它。如果事实证明这是用例的要求,那么尝试
session.merge
干杯!!
关于java - org.hibernate.NonUniqueObjectException。如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18702715/
什么是 hibernate 和n- hibernate ?我可以在 Visual Studio 2008 中使用它进行 C# Web 应用程序开发吗?请给我建议...我是 asp.net Web 应用
我有一个不系统地发生的异常(exception)。 我试图通过在每次迭代中刷新和清理 session 来解决此问题,但没有成功。 [quartzScheduler_Worker-7] ERROR jd
使用 Hibernate 在数据库中存储 IP 地址的最佳类型是什么? 我虽然是 Byte[] 或 String,但有没有更好的方法,或者你用什么? @Column(name = "range_fr
我正在尝试制定一个公式来选择用户个人资料的用户友好名称。它选择名字 + ' ' + 姓氏 如果其中至少有一个不为空且不为空(包含非空白字符),否则选择 短名称 (条件相同),最后,如果 短名称 为空或
在hibernate中,是否可以将鉴别器作为一个实体?例如,如果我将 Department 作为基类,将 AdminDepartment 和 ProcessingDepartment 作为子类。 De
我只想从表中获取一些列值。因此,我已经使用投影来实现这一目标。该代码有效,但我认为它无效。 我的问题是当我使用ProjectionsList并将标准条件列表设置为ArrayList时-Bulletin
你好: 我对 hibernate 缓存缓存的内容感到困惑。 从文档中,我知道 hibernate 中有缓存类型。 一级 :交易级别。 似乎要被 session 持久化的实体被缓存在这里。 二级缓存 :
我遇到了一个情况: save或update hibernate 的目标表中的某些数据 在目标表上有一个触发器,该触发器将在目标表的insert或update操作之前执行 由 hibernate 将此记
我有一个名为 Master_Info_tbl 的表。它是一个查询表: 这是该表的代码: @Entity @Table(name="MASTER_INFO_T") public class Code
我想知道如何在 Hibernate 查询语言中使用日期文字。我在我的 JPA 项目中做了如下操作(作为 Eclipselink 提供者)并且它工作正常。 SELECT m FROM Me m WHER
@Entity public class Troop { @OneToMany(mappedBy="troop") public Set getSoldiers() { ...
我正在尝试使用 hibernate 查询删除表 'user_role' 中的所有行。但每次我都会出错。有人可以帮我吗。 DaoImpl @Override public void deleteAll(
不是将数据库操作分散在四个 (osgi) 包中,而是在那里做略有不同的事情。我想创建一个负责所有持久性问题的(简单的)OSGi 包。我觉得这并不像听起来那么简单,因为“每个包都有独特的类加载器”。 因
这就是我使用生成器的方式: private Integer id; 我看到的行为是: 创建第一个对象 hibernate 分配 id = 1 删除该对象 关闭服务
对象级别的实体和值类型有什么区别。我知道实体将有一个 id 但值不会,但为什么我们需要不同的方法来映射实体与值类型? 这样做是为了让hibernate可以对值类型应用任何优化吗? 最佳答案 一个实体已
我正在使用 HibernateTemplate.findByCriteria 方法进行一些查询。现在我想在标准上创建一些 SQL 限制,比如 criteria.add(Restrictions.sql
所以我有以下代码: Query query = session.createQuery("from Weather"); List list = query.list();
如何使用Hibernate映射具有多个实体的 View ? 问候, 混沌 最佳答案 请参见Hibernate文档中第5.1.3节“类”,紧接在“Id”节之前: There is no differen
据我所知,Hibernate 有两种类型的实现 JPA的实现(2)(@Entity,@Table注解) 扩展到旧的(传统的) hibernate (没有 JPA),使用 HSQL 查询,没有注释 如果
我需要一个将条目存储为键值对的集合(因此我可以通过键查找值),但我需要一个允许多个值使用 hibernate 共享同一个键的集合 最佳答案 一个键具有多个值的映射称为多映射 - 在 Apache 公共
我是一名优秀的程序员,十分优秀!