我正在尝试在我的 mysql 数据库中插入一条新记录;Hibernate insert 语句在日志中打印,但数据库不插入新记录。没有发布任何错误,但我的 rpcService 调用打印了我的 onFailure 消息。另外:我的应用程序从数据库中读取数据很好,所以我的应用程序的其他部分运行良好。下面是代码和配置:
休眠.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">xxxxxxxx</property>
<property name="hibernate.connection.url">jdbc:mysql://xxxxxxxxx.us-west-1.rds.amazonaws.com/xxxxxxx</property>
<property name="hibernate.connection.username">xxxxxx</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- configuration pool via c3p0-->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">20</property>
<property name="c3p0.max_statements">20</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">1800</property> <!-- seconds -->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<!-- <property name="hbm2ddl.auto">update</property>-->
<mapping class="com.local.shared.School"/>
<mapping class="com.local.shared.SchoolVideo"/>
<mapping class="com.local.shared.Category"/>
<mapping class="com.local.shared.Administrator"/>
</session-factory>
</hibernate-configuration>
rpcService 调用:
@Override
public void doUpdateSchoolVideo(SchoolVideo schoolVideo) {
rpcService.updateSchoolVideo(schoolVideo,
new AsyncCallback<SchoolVideo>() {
public void onFailure(Throwable caught) {
caught.printStackTrace();
new Exception().printStackTrace();
Window.alert("Couldn't update schoolVideo info!");
}
public void onSuccess(SchoolVideo schoolVideo) {
System.out.println("SchoolVideo table updated!");
loadSubmittedVideos(Utilities.getSchoolPage()
.getSchool_name());
}
});
}
rpcService 实现:
@Override
public SchoolVideo updateSchoolVideo(SchoolVideo schoolVideo) {
System.out.println("Attempting to save the schoolVideo information"
+ "\n");
Transaction tx = null;
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
HttpSession httpSession = httpServletRequest.getSession(false);
School sessionSchool = (School) httpSession.getAttribute("school");
System.out.println("\n updateSchoolVideo school in session is " +
" = " + sessionSchool.getSchool_name() + "\n");
try {
tx = session.beginTransaction();
School school = sessionSchool;
System.out.println("\n cachedSchool = " + school + "\n");
School querySchool = (School) session.get(School.class,
new Integer(school.getId()));
System.out.println(querySchool.getId() + " equals id");
schoolVideo.setSchool_id(querySchool);
schoolVideo.setReviewed(1);
System.out.println(schoolVideo.getVideo_name() + " videoname");
session.save(schoolVideo);
System.out.println("\n Video Should have been saved in database after upload \n");
tx.commit();
} catch (RuntimeException e) {
if (tx != null && tx.isActive()) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
throw e;
}
}
return schoolVideo;
}
我的登录语句显示 Hibernate 插入: Hibernate: insert into SchoolVideo (category, image_name, review, school_id, video_name) values (?, ?, ?, ?, ?)
因此,我的 rpcServiceImpl 方法一直执行到 session.save() 方法调用。直接在它不打印之后的打印语句,所以我知道这就是一切都死的地方。任何人都知道我现在如何跟踪这种缺少 db insert 的情况。