Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.sql.SQLException:使用HSQL和Spring不支持此函数_Java_Spring_Hibernate_Hsqldb - Fatal编程技术网

java.sql.SQLException:使用HSQL和Spring不支持此函数

java.sql.SQLException:使用HSQL和Spring不支持此函数,java,spring,hibernate,hsqldb,Java,Spring,Hibernate,Hsqldb,有人能告诉我为什么我要获取java.sql.SQLException:使用HSQL和Spring不支持此函数吗?我正试图在数据库中插入新行 下面是我的DAO,我在mySession.save(消息)行中得到错误: 这是我的数据库配置: public class DatabaseConfig { private static final Logger LOGGER = getLogger(DatabaseConfig.class); @Autowired Enviro

有人能告诉我为什么我要获取java.sql.SQLException:使用HSQL和Spring不支持此函数吗?我正试图在数据库中插入新行

下面是我的DAO,我在mySession.save(消息)行中得到错误:

这是我的数据库配置:

public class DatabaseConfig
{

    private static final Logger LOGGER = getLogger(DatabaseConfig.class);


    @Autowired
    Environment env;

    @Bean
    public DataSource dataSource() {

        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).
        addScript("schema.sql").build();

        return db;
    }


    @Bean
    public DataSource hsqlDataSource()  {

        BasicDataSource ds = new BasicDataSource();


        try {
            ds.setDriverClassName("org.hsqldb.jdbcDriver");
            ds.setUsername("sa");
            ds.setPassword("");
            ds.setUrl("jdbc:hsqldb:mem:mydb");
        }
        catch (Exception e)
        {
            LOGGER.error(e.getMessage());
        }
        return ds;



    }

    @Bean
    public SessionFactory sessionFactory()
    {

        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        factoryBean.setDataSource(hsqlDataSource());
        factoryBean.setHibernateProperties(getHibernateProperties());
        factoryBean.setPackagesToScan(new String[]{"com.xxxxx.HelloSpringJavaBasedJavaConfig.model"});

        try
        {
            factoryBean.afterPropertiesSet();
        } catch (IOException e)
        {
            LOGGER.error(e.getMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        return factoryBean.getObject();
    }

    @Bean
    public Properties getHibernateProperties()
    {
        Properties hibernateProperties = new Properties();

        hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
        hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        hibernateProperties.setProperty("hibernate.use_sql_comments", env.getProperty("hibernate.use_sql_comments"));
        hibernateProperties.setProperty("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));

        hibernateProperties.setProperty("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics"));

        hibernateProperties.setProperty("javax.persistence.validation.mode", env.getProperty("javax.persistence.validation.mode"));

        //Audit History flags
        hibernateProperties.setProperty("org.hibernate.envers.store_data_at_delete", env.getProperty("org.hibernate.envers.store_data_at_delete"));
        hibernateProperties.setProperty("org.hibernate.envers.global_with_modified_flag", env.getProperty("org.hibernate.envers.global_with_modified_flag"));

        return hibernateProperties;
    }

    @Bean
    public HibernateTransactionManager hibernateTransactionManager()
    {
        HibernateTransactionManager htm = new HibernateTransactionManager();
        htm.setSessionFactory(sessionFactory());
        htm.afterPropertiesSet();
        return htm;
    }


}
下面是我在控制台上看到的内容:

Exception in thread "main" org.hibernate.AssertionFailure: null id in com.xxx.HelloSpringJavaBasedJavaConfig.model.Message entry (don't flush the Session after an exception occurs)
以下是我的消息模型bean:

@Entity
@Table(name = "messages")
public class Message
{

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;

    @Column(name = "message")
    private String message;

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    @Override
    public String toString()
    {
        return "Message{" +
                "id='" + id + '\'' +
                ", message='" + message + '\'' +
                '}';
    }
}

不能将
字符串
@GenerateValue
与策略
GenerationType.AUTO
一起使用,因为它使用序列生成器,而不能与非数值一起使用。你必须自己设置。如果希望为您生成一个
整数

或者使用使用字符串值的id生成器

@Id 
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")

不能将
字符串
@GenerateValue
与策略
GenerationType.AUTO
一起使用,因为它使用序列生成器,而不能与非数值一起使用。你必须自己设置。如果希望为您生成一个
整数

或者使用使用字符串值的id生成器

@Id 
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")

这个问题很可能与试图使用已经发出错误信号的会话有关。正如Sotirios所提到的,在DAO中捕获异常是一个坏主意,如果这样做了,则重新抛出异常是至关重要的

通常,一旦捕获到Hibernate异常,必须回滚事务并关闭会话,因为会话状态可能不再有效()

如果会话引发异常(包括任何SQLException),请立即回滚数据库事务,调用Session.close()并放弃会话实例。某些会话方法不会使会话保持一致状态。Hibernate引发的任何异常都不能视为可恢复的。通过在finally块中调用close()确保会话将被关闭


因为您使用的是Spring,所以大多数都不适用于您,但您看到的异常消息表明,问题的实际原因可能与捕获异常然后继续使用同一会话有关。

此问题很可能与尝试使用已发出错误信号的会话有关。正如Sotirios所提到的,在DAO中捕获异常是一个坏主意,如果这样做了,则重新抛出异常是至关重要的

通常,一旦捕获到Hibernate异常,必须回滚事务并关闭会话,因为会话状态可能不再有效()

如果会话引发异常(包括任何SQLException),请立即回滚数据库事务,调用Session.close()并放弃会话实例。某些会话方法不会使会话保持一致状态。Hibernate引发的任何异常都不能视为可恢复的。通过在finally块中调用close()确保会话将被关闭


由于您使用的是Spring,大多数情况下您都不适用,但您看到的异常消息表明,问题的实际原因可能与捕获异常然后继续使用同一会话有关。

这是一个版本问题。我更新了版本,现在一切正常

这是一个版本问题。我更新了版本,现在一切正常

这与正在使用的hsql版本有关可能导致问题的版本是hibernate 4的1.8版本。需要改用2.2.9这与正在使用的hsql版本有关,可能导致问题的版本是1.8和hibernate 4。需要改用2.2.9版本

在将hibernate升级到4.2.8版本后,我遇到了同样的问题。查看日志,我注意到hibernate生成的sql查询试图插入一个主键为空的记录。该字段仅用@Id@GeneratedValue注释


将hsqldb升级到2.2.9版使其消失,正如赛义德所说,我非常感谢他的回复。

在将hibernate升级到4.2.8版后,我遇到了同样的问题。查看日志,我注意到hibernate生成的sql查询试图插入一个主键为空的记录。该字段仅用@Id@GeneratedValue注释


将hsqldb升级到2.2.9版使其消失,正如所说的那样,我非常感谢他的回复。

您有一个
null
id。显示
消息
类实现。另外,不要在DAO中捕获异常。@SotiriosDelimanolis ID是“@GeneratedValue(strategy=GenerationType.AUTO)”,因此我不必在itI中输入值。我只是在github上发布了源代码,以便您可以尝试itI。我没有访问权限。不要这样做,在这里发布您的代码或错误。您有一个
null
id。显示您的
消息
类实现。另外,不要在DAO中捕获异常。@SotiriosDelimanolis ID是“@GeneratedValue(strategy=GenerationType.AUTO)”,因此我不必在itI中输入值。我只是在github上发布了源代码,以便您可以尝试itI。我没有访问权限。不要这样做,在这里发布你的代码或错误。这不是问题所在,但我在github上发布了源代码,所以你可以试试it@SJS什么告诉你这不是问题?这不是问题,但我在github上发布了源代码,所以你可以试试it@SJS什么告诉你这不是问题?
@Id 
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")