Hibernate更新HSQLDB数据库的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate更新HSQLDB数据库的问题相关的知识,希望对你有一定的参考价值。

如何来更新后台数据库信息?更新的信息是在多个Text中输入后得到,因为我要接收一个课程表。以周一课程为例做了8个Text,从中得到不同的课程更新数据库里面原有的课程。这个监听是做在一个button上。
Hibernate了解的还不多,希望能有大大把代码写点注释,先谢谢了~

就是 连接数据库 的

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序实用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。
参考技术A 就是 连接数据库 的

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序实用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。
参考技术B 先把要修改的数据load出来,然后在presistent状态现set就好了!!gogejobs@gmail.com

Hibernate 不在内存数据库中使用 H2 创建表

【中文标题】Hibernate 不在内存数据库中使用 H2 创建表【英文标题】:Hibernate is not creating tables with H2 in memory database 【发布时间】:2016-11-16 15:02:55 【问题描述】:

我正在尝试测试一个将 SpringMVC 和 Hibernate 与 MySQL 结合使用的应用程序。我尝试使用 HSQLDB,但由于语法与 MySQL 不同,查询可能不起作用,所以我决定去 H2。 问题是当我运行它时它说“找不到表 MAILCONFIG”,如果我在 INIT 语法上创建它说它已经存在。

我已经配置了一个不做任何事情的简单测试,我只想让它运行。

我有以下文件:

ServiceTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "file:src/test/resources/applicationContext.xml")
@Transactional
@WebAppConfiguration
public class ServiceTest 

private static DataSource ds;

@BeforeClass
public static void setUpConnection()
    ds = new DataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:testDB");
    ds.setUsername("sa");
    ds.setPassword("");
    HibernateConfiguration.dataSourceTest(ds);


@AfterClass
public static void cleanConnection()
    HibernateConfiguration.dataSourceTest(null);


applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
    <property name="persistenceUnitName" value="testingSetup" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
</bean>

<context:component-scan base-package="com.adistec" />
<context:annotation-config/>

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">
<persistence-unit name="testingSetup" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
        <property name="hibernate.connection.url" value="jdbc:h2:mem:testDB;DB_CLOSE_DELAY=-1;MODE=MySQL" />
        <property name="hibernate.connection.username" value="sa" />
        <property name="hibernate.connection.password" value="" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
        <property name="hibernate.archive.autodetection" value="class, hbm"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
    </properties>
</persistence-unit>

MailConfig.java

@Entity
public class MailConfig 
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  Long id;
  @Column
  String host;

我在测试中指定了 Hibernate 配置,因为应用程序使用 java 类而不是 XML 文件。

谁能帮我解决这个问题?或者,如果也欢迎使用 java 类测试它的更简单方法,我还没有找到解决方案。 我希望它在本地工作,然后它也必须与 jenkins 一起工作,这样它就可以创建无法在 VM 上执行的操作。

谢谢!

【问题讨论】:

HibernateConfiguration.dataSourceTest(ds); 让我不寒而栗。但是,您应该将HibernateJpaVendorAdaptergenerateDdl 属性设置为true,否则不会生成任何内容。 【参考方案1】:

您的 MailConfig 类中似乎缺少 @Table 注释。

您可以从 http://mycuteblog.com/h2-database-example-hibernate-spring-boot/ 找到使用 spring、hibernate 和 h2 数据库的完整工作示例

【讨论】:

@Table 是可选的。【参考方案2】:

在主类中添加@EntityScan(basepackage="com.entity")注解

【讨论】:

OP 没有使用 Spring Boot,因此建议在非 Spring Boot 项目上添加 Spring Boot 注释无济于事。

以上是关于Hibernate更新HSQLDB数据库的问题的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate更新HSQLDB数据库的问题

使用 Hibernate 更新 HSQLDB 上的 LOB/BLOB 值会产生数据异常

使用 Hibernate 和 HSQLDB 找不到表

HSQLDB + JUnit + Hibernate:java.sql.SQLException:无效的模式名称

Hibernate Update 在 HSQLDB 中转换删除/插入

带有 Hibernate 4.3.6 的 HSQLDB 2.3.2 中的语法错误