我有一个子类,它覆盖了超类的getter(因为它添加了不同的行为)。我正在使用@AttributeOverride,但它会抛出错误 org.hibernate.MappingException:com.hbmap.domain.Product 中找到的名称的重复属性映射。
我不确定我是否做错了。
产品:
@Entity
@Table(name = "product")
@Access(AccessType.PROPERTY)
@AttributeOverride(name = "name", column = @Column(name = "name") )
public class Product extends Properties {
private Long id;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String getName() {
// Do something different than superclass
return super.getName();
}
}
属性(超类):
@MappedSuperclass
@Access(AccessType.PROPERTY)
public class Properties {
private String name;
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
已编辑1
完整的堆栈跟踪:
org.hibernate.MappingException:在com.hbmap.domain.Product中找到的名称的重复属性映射 在org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:515) 在org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:505) 在org.hibernate.mapping.RootClass.validate(RootClass.java:270) 在org.hibernate.cfg.Configuration.validate(Configuration.java:1360) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930) 在org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372) 在org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454) 在org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 在org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663) 在org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629) 在org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677) 在org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548) 在org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489) 在org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 在javax.servlet.GenericServlet.init(GenericServlet.java:158) 在org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231) 在org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144) 在org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4914) 在org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201) 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1409) 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1399) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) 在java.lang.Thread.run(未知来源)
答案 0 :(得分:1)
我找到了一个解决方案,我不知道它是否是最好的,但它确实有效...... 将 @Transient 添加到子类中的getter。
<强>产品:强>
@Entity
@Table(name = "product")
@Access(AccessType.PROPERTY)
@AttributeOverride(name = "name", column = @Column(name = "name") )
public class Product extends Properties {
private Long id;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
@Transient
public String getName() {
// Do something different than superclass
return super.getName();
}
}
答案 1 :(得分:0)
此处的问题是,您在getName()
类中覆盖Product
方法,该方法将重复使用@Column
注释两次,并尝试为{{1}创建两个列}。
这就是你获得例外的原因:
org.hibernate.MappingException:在com.hbmap.domain.Product中找到的名称的重复属性映射
所以只需从name
类中删除Overriden方法,因为它会自动从超级类中获取名称,因为Product
会为你做这个,删除它:
Inheritance
修改强>
如果您需要覆盖它,使用带有覆盖方法的@Override
public String getName() {
return super.getName();
}
将在映射中跳过此新列。