这是我的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
答案 0 :(得分:0)
主要问题在亲爱的 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);
}