【问题标题】:Repeated column in mapping for entity: com.priyan.patients.EPrescriber column: id (should be mapped with insert="false" update="false")实体映射中的重复列:com.priyan.patients.EPrescriber 列:id(应使用 insert="false" update="false" 映射)
【发布时间】:2013-12-07 02:13:27
【问题描述】:

这是我的 bean 类

@Entity
@Table(name="TABLETS", uniqueConstraints = {@UniqueConstraint(columnNames = "tradeName")})
public class Tablets implements Serializable{

    private static final long serialVersionUID = 4854785134773287611L;

    public Tablets() {
    }

    public Tablets(int id, String tradeName, String category, String type,
            Set<EPrescriber> selectedTablets) {
        this.id = id;
        this.tradeName = tradeName;
        this.category = category;
        this.type = type;
        this.selectedTablets = selectedTablets;
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @SequenceGenerator(name="tablets_seq", sequenceName="tablets_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE , generator= "tablets_seq" )
    private int id;

    @Column private String tradeName;
    @Column private String category;
    @Column private String type;


    @OneToMany(fetch = FetchType.LAZY, mappedBy="tablets")
    @Column private Set<EPrescriber> selectedTablets=new HashSet<EPrescriber>(0);

    public Set<EPrescriber> getSelectedTablets() {
        return selectedTablets;
    }

    public void setSelectedTablets(Set<EPrescriber> selectedTablets) {
        this.selectedTablets = selectedTablets;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTradeName() {
        return tradeName;
    }
    public void setTradeName(String tradeName) {
        this.tradeName = tradeName;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

}

这里还有另一个 bean 类

@Entity
@Table(name = "E_PRESCRIBER")
public class EPrescriber implements Serializable {

    private static final long serialVersionUID = 440529869955257543L;

    @Id
    @Column(name = "id" ,unique = true, nullable = false)
    @SequenceGenerator(name = "ePrescriber_seq", sequenceName = "ePrescriber_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ePrescriber_seq")
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", nullable = false)
    private Tablets tablets;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Tablets getTablets() {
        return tablets;
    }

    public void setTablets(Tablets tablets) {
        this.tablets = tablets;
    }

}

这是我的错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactsControllers': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.priyan.patients.ContactsDAO com.priyan.patients.ContactsControllers.contactsDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactsDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.priyan.patients.ContactsDAO.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.priyan.patients.EPrescriber column: id (should be mapped with insert="false" update="false")
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.priyan.patients.ContactsDAO com.priyan.patients.ContactsControllers.contactsDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactsDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.priyan.patients.ContactsDAO.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.priyan.patients.EPrescriber column: id (should be mapped with insert="false" update="false")

请帮我解决这个问题。 谢谢

编辑

现在我改变了我的 bean 类如下

@Entity
@Table(name = "E_PRESCRIBER")
public class EPrescriber implements Serializable {

    private static final long serialVersionUID = 440529869955257543L;

    @Id
    @Column(name = "ePrescriberid"  ,unique = true, nullable = false)
    @SequenceGenerator(name = "ePrescriber_seq", sequenceName = "ePrescriber_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ePrescriber_seq")
    private int ePrescriberid;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", nullable = false)
    private Tablets tablets;

    public int getePrescriberid() {
        return ePrescriberid;
    }

    public void setePrescriberid(int ePrescriberid) {
        this.ePrescriberid = ePrescriberid;
    }

    public Tablets getTablets() {
        return tablets;
    }

    public void setTablets(Tablets tablets) {
        this.tablets = tablets;
    }

}

现在它给出了以下错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactsControllers': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.priyan.patients.EPrescriberDAO com.priyan.patients.ContactsControllers.ePrescriberDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'EPrescriberDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.orm.hibernate3.HibernateTemplate com.priyan.patients.EPrescriberDAO.hibernateTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.orm.hibernate3.HibernateTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.priyan.patients.EPrescriberDAO com.priyan.patients.ContactsControllers.ePrescriberDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'EPrescriberDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.orm.hibernate3.HibernateTemplate com.priyan.patients.EPrescriberDAO.hibernateTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.orm.hibernate3.HibernateTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework

【问题讨论】:

  • 我认为这是你的问题:@JoinColumn(name = "id", nullable = false) 因为你已经有一个名为 column 的 id。
  • 亲爱的豪尔赫·坎波斯。感谢您的建议。现在问题已得到纠正,但出现另一个错误请帮我解决此问题 [POST 已编辑]
  • 很高兴它有帮助。正如 StackTrace 所说,没有将 DAO 定义为 EPrescriberDAO。您应该发布您的 bean 映射器,以便我们可以看到您的配置。最后,这里已经很晚了,几乎是凌晨 00:00,所以,我现在要去睡觉了。如果有人在明天之前帮助你,我会继续帮助你。
  • 晚安,亲爱的 Jorge.u r 正确。似乎我的 DAO 有问题。将检查这一点

标签: spring hibernate postgresql spring-mvc nhibernate-mapping


【解决方案1】:

在亲爱的Jorge

的帮助下整理了主要问题

我的 DAO 的另一个第二个问题

现在用下面的代码排序

@Autowired
private SessionFactory sessionFactory;

public void getTabletbyNameAndSave(String []selectedMedicines) {
    Session session =  HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    EPrescriber ePrescriber=new EPrescriber();
    List<Tablets> selectedTablets=new ArrayList<Tablets>();
    //MY transaction & business logics
    session.save(ePrescriber);
    session.getTransaction().commit();
    //sessionFactory.getCurrentSession().save(ePrescriber);
}

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    相关资源
    最近更新 更多