数据未保存在数据库中 [英] Data not getting saved in database
问题描述
这是我的bean.xml
<?xml version =1.0encoding =UTF-8? >
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:context =http://www.springframework.org/schema/context
xmlns:p =http://www.springframework.org/schema/p
xmlns :tx =http://www.springframework.org/schema/tx
xsi:schemaLocation =
http://www.springframework.org/schema/tx
http: //www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ spring-tx-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http ://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd\">
<! - 打开AspectJ @Configurable支持 - >
< context:spring-configured />
< context:property-placeholder location =classpath *:*。properties/>
< context:component-scan base-package =your.intermedix/>
< context:annotation-config />
<! - 打开@Autowired,@PostConstruct等支持 - >
< bean class =org.springframework.context.annotation.CommonAnnotationBeanPostProcessor/>
< bean id =mySessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =myDataSource/>
< property name =annotatedClasses>
< list>
< value> your.intermedix.domain.Contact< / value>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto>建立< / prop>
< /道具>
< / property>
< / bean>
< bean id =myDataSourceclass =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =usernamevalue =monty/>
< property name =passwordvalue =indian/>
< / bean>
< tx:annotation-driven transaction-manager =txManager/>
< bean id =txManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =mySessionFactory/>
< / bean>
< / beans>
My Service class
打包your.intermedix.services;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
导入your.intermedix.domain.Contact;
导入your.intermedix.services.IContact;
@Service
@Transactional
public class ContactSerImpl扩展HibernateDaoSupport实现IContact {
public联系人saveContact(联系人联系人){
System。通过out.println(接触);
getHibernateTemplate()。saveOrUpdate(contact);
退货联系人;
}
public void hello(){
System.out.println(Hello Guru);
$ / code>
我的前端
package your.intermedix;
import java.util.logging.Logger;
导入your.intermedix.domain.Contact;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.data.util.BeanItem;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.UserError;
import com.vaadin.ui.Window;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Controller;
导入your.intermedix.domain.ContactValdiation;
导入your.intermedix.services.IContact;
$ b / **
*应用程序的主类
* /
@SuppressWarnings(serial)
@Configurable( preConstruction = true)
@Controller
public class MyVaadinApplication extends Application实现Button.ClickListener
{
$ b $ @Autowired
私有瞬态IContact icontact;
私人窗口窗口;
private Button save = new Button(Save,(ClickListener)this);
private Button cancel = new Button(Cancel,(ClickListener)this);
private联系联系人= new Contact();
private Form form = new Form();
private BeanItem item = new BeanItem(contact);
private GridLayout ourLayout;
public void init(){
loadForm();
private void loadForm(){
window = new Window(InterMedix Application);
setMainWindow(window);
final Panel面板=新面板(联系信息);
panel.addStyleName(panelexample);
//默认情况下,面板的宽度为100%,使其为
//缩小以适合内容。
panel.setWidth(Sizeable.SIZE_UNDEFINED,0);
form.setWriteThrough(false);
form.setInvalidCommitted(false);
form.setImmediate(true);
form.setFormFieldFactory(new ContactValdiation());
form.setRequired(true);
form.setItemDataSource(item);
form.setVisibleItemProperties(new Object [] {name,lastname,email,designation,date,comments});
form.setValidationVisibleOnCommit(true);
form.setFooter(new VerticalLayout());
//在页脚中有一个按钮栏。
HorizontalLayout buttons = new HorizontalLayout();
form.getLayout()。addComponent(buttons);
buttons.addComponent(save);
buttons.addComponent(cancel);
panel.addComponent(form);
window.addComponent(panel);
$ b public void buttonClick(ClickEvent event){
if(event.getSource()== save){
尝试{
form.commit();
icontact.hello();
icontact.saveContact(contact);
}
catch(Exception e){
}
}
}
}
错误如下:
2011-01-04 21 :57:46.331:WARN ::上下文启动失败org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@14ce5eb {/ sampleproject,file:/ C:/ projects / sampleproject / src / main / webapp /; file:/ C:/ projects / sampleproject / target / sampleproject-1.0 /;}
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'myVaadinApplication'的Bean时出错:通过bean属性'user'表达的不满意的依赖关系:没有唯一的bean类型[java.lang.Object]被定义为:预期的单个匹配的bean,但发现15:[org.springframework.context.config.internalBeanConfigurerAspect,org.springframework.beans.factory.config.PropertyPlace
holderConfigurer#0,contactSerImpl, org.springframework.context.annotation.internal RequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.CommonAnnotationBea
nPostProcessor#0,mySessionFactory ,myDataSource,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.config.internalTransactionAdvisor,txManager,messageSource,applicationEventMulticaster]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory。 java:1119)
解决方案我认为你多次做了几件事:
这:
< context:annotation-config />
已包含此项:
<! - 打开@Autowired,@PostConstruct等支持 - >
< bean class =org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor/>
< bean class =org.springframework.context.annotation.CommonAnnotationBeanPostProcessor/>
移除
< bean>
定义,并再试一次,也许这就是原因。
参考:基于注解的容器配置
更新:关于您的新错误: //static.springsource.org/spring/docs/3.0.x/javadoc-api/index.html?org/springframework/orm/hibernate3/support/HibernateDaoSupport.htmlrel =nofollow>
HibernateDaoSupport
需要HibernateTemplate
或SessionFactory
作为依赖关系。你有一个SessionFactory
,但Spring无法连接它,因为默认情况下,Spring不会自动连接属性,所以你需要手动连接属性或设置< beans default-autowire =byType>
(如果使用byName
,则必须更改SessionFactory bean定义至id =sessionFactory
)Here is my bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Turn on AspectJ @Configurable support --> <context:spring-configured /> <context:property-placeholder location="classpath*:*.properties" /> <context:component-scan base-package="your.intermedix"/> <context:annotation-config/> <!-- Turn on @Autowired, @PostConstruct etc support --> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="annotatedClasses"> <list> <value>your.intermedix.domain.Contact</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"/> <property name="username" value="monty"/> <property name="password" value="indian"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="mySessionFactory" /> </bean> </beans>
My Service class
package your.intermedix.services; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import your.intermedix.domain.Contact; import your.intermedix.services.IContact; @Service @Transactional public class ContactSerImpl extends HibernateDaoSupport implements IContact { public Contact saveContact(Contact contact) { System.out.println(contact); getHibernateTemplate().saveOrUpdate(contact); return contact; } public void hello() { System.out.println("Hello Guru"); } }
My Frontend
package your.intermedix; import java.util.logging.Logger; import your.intermedix.domain.Contact; import com.vaadin.Application; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.Form; import com.vaadin.ui.GridLayout; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Panel; import com.vaadin.ui.VerticalLayout; import com.vaadin.data.util.BeanItem; import com.vaadin.terminal.Sizeable; import com.vaadin.terminal.UserError; import com.vaadin.ui.Window; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.stereotype.Controller; import your.intermedix.domain.ContactValdiation; import your.intermedix.services.IContact; /** * The Application's "main" class */ @SuppressWarnings("serial") @Configurable(preConstruction = true) @Controller public class MyVaadinApplication extends Application implements Button.ClickListener { @Autowired private transient IContact icontact; private Window window; private Button save = new Button("Save", (ClickListener) this); private Button cancel = new Button("Cancel", (ClickListener) this); private Contact contact = new Contact(); private Form form = new Form(); private BeanItem item = new BeanItem(contact); private GridLayout ourLayout; public void init(){ loadForm(); } private void loadForm(){ window = new Window("InterMedix Application"); setMainWindow(window); final Panel panel = new Panel("Contact Information"); panel.addStyleName("panelexample"); // The width of a Panel is 100% by default, make it // shrink to fit the contents. panel.setWidth(Sizeable.SIZE_UNDEFINED, 0); form.setWriteThrough(false); form.setInvalidCommitted(false); form.setImmediate(true); form.setFormFieldFactory(new ContactValdiation()); form.setRequired(true); form.setItemDataSource(item); form.setVisibleItemProperties(new Object[] {"name","lastname","email","designation","date","comments"}); form.setValidationVisibleOnCommit(true); form.setFooter(new VerticalLayout()); // Have a button bar in the footer. HorizontalLayout buttons = new HorizontalLayout(); form.getLayout().addComponent(buttons); buttons.addComponent(save); buttons.addComponent(cancel); panel.addComponent(form); window.addComponent(panel); } public void buttonClick(ClickEvent event) { if (event.getSource() == save) { try { form.commit(); icontact.hello(); icontact.saveContact(contact); } catch (Exception e) { } } } }
The Error below:
2011-01-04 21:57:46.331:WARN::Failed startup of context org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@14ce5eb{/sampleproject,file:/C:/projects/sampleproject/src/main/webapp/;file:/C:/projects/sampleproject/target/sampleproject-1.0/;} org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myVaadinApplication': Unsatisfied dependency expressed through bean property 'user': No unique bean of type [java.lang.Object] is defined: expected single matching bean but found 15: [org.springframework.context.config.internalBeanConfigurerAspect, org.springframework.beans.factory.config.PropertyPlace holderConfigurer#0, contactSerImpl, org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, org.springframework.context.annotation.internalPersistenceAnnotationProcessor, org.springframework.context.annotation.CommonAnnotationBea nPostProcessor#0, mySessionFactory, myDataSource, org.springframework.aop.config.internalAutoProxyCreator, org.springframework.transaction.config.internalTransactionAdvisor, txManager, messageSource, applicationEventMulticaster] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1119)
解决方案I think you are doing several things multiple times:
This:
<context:annotation-config/>
already includes this:
<!-- Turn on @Autowired, @PostConstruct etc support --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Remove the
<bean>
definitions and try again, perhaps that's the reason.Reference: Annotation-based container configuration
Update: about your new error:
HibernateDaoSupport
needs either aHibernateTemplate
or aSessionFactory
as dependency. You have aSessionFactory
, but Spring can't wire it because by default, Spring doesn't autowire properties, so you need to either manually wire the property or set<beans default-autowire="byType">
(if you usebyName
, you will have to change your SessionFactory bean definition toid="sessionFactory"
)这篇关于数据未保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!