Java 在hibernate中插入实体问题
我目前正在尝试将hibernate与spring集成,我使用dao设计模式和mysql作为数据库。我正在尝试在数据库中添加联系人实体,但未添加,也未显示任何错误。顺便说一下,我可以获取联系人列表并按id获取,但无法更新或插入 这是我的上下文文件Java 在hibernate中插入实体问题,java,spring,hibernate,spring-mvc,jakarta-ee,Java,Spring,Hibernate,Spring Mvc,Jakarta Ee,我目前正在尝试将hibernate与spring集成,我使用dao设计模式和mysql作为数据库。我正在尝试在数据库中添加联系人实体,但未添加,也未显示任何错误。顺便说一下,我可以获取联系人列表并按id获取,但无法更新或插入 这是我的上下文文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mailmaneger" />
<property name="username" value="root" />
<property name="password" value="" />
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="packagesToScan" value="biz.picosoft.entity"/>
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
<property name="checkWriteOperations" value="false"></property>
</bean>
<bean id="d" class="biz.picosoft.daoImpl.ContacteDaoImpl">
<property name="template" ref="template"></property>
</bean>
</beans>
我的ContactedAOIP文件
package biz.picosoft.daoImpl;
import java.util.List;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;
import biz.picosoft.entity.Contacte;
@Transactional(readOnly=false)
public class ContacteDaoImpl extends GenericDaoImp<Contacte> implements ContacteDao{
}
请添加以下内容及其命名空间:
xmlns:tx="http://www.springframework.org/schema/tx"
和在模式位置:
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
<!-- This tells Spring to activate annotation-driven transactions -->
<tx:annotation-driven/>
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
您可以删除(readOnly=false),因为默认情况下它是false
参考:
在哪里调用insert或update语句?main方法中的代码只是获取所有细节,在创建contact对象时,没有调用insert或update。否当我使用insert更改findall时,我没有得到任何嵌套的异常是org.xml.sax.SAXParseException;行号:49;栏目号:26;cvc complex type.2.4.c:Le caractère générique concordant est strict,mais aucune déclaration ne peutètre trouvée pour lélément'tx:注释驱动'我在添加时出错,您真的添加了
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
是否要将其连接到架构位置?
package biz.picosoft.mains;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import biz.picosoft.daoImpl.ContacteDaoImpl;
import biz.picosoft.entity.Contacte;
public class TestHibernate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Contacte contacte=new Contacte("fatma", "test2", "test", "test");
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
contacte.setIdContact (4);
ContacteDaoImpl contacteDaoImpl=(ContacteDaoImpl) context.getBean("d");
System.out.println( contacteDaoImpl.findAll().size());
contacteDaoImpl.insert(contacte);
}
}
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
<!-- This tells Spring to activate annotation-driven transactions -->
<tx:annotation-driven/>