gpt4 book ai didi

java.sql.SQLException : This function is not supported using HSQL and Spring 异常

转载 作者:行者123 更新时间:2023-11-29 09:41:18 28 4
gpt4 key购买 nike

有人能告诉我为什么会出现 java.sql.SQLException: This function is not supported using HSQL and Spring 吗?我正在尝试向我的数据库中插入一个新行..

下面是我的 DAO,我在 mySession.save(message) 行收到错误:

@Transactional
@Repository
public class MessageDaoImpl implements MessageDao
{


private Log log = null;
@Autowired
private SessionFactory sessionFactory;

public MessageDaoImpl()
{
super();
log = LogFactory.getLog(MessageDaoImpl.class);

}

@SuppressWarnings("unchecked")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public List<Message> listMessages()
{
try
{
return (List<Message>) sessionFactory.getCurrentSession()
.createCriteria(Message.class).list();

} catch (Exception e)
{
log.fatal(e.getMessage());
return null;
}
}


@SuppressWarnings("unchecked")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void SaveOrUpdateMessage(Message message)
{
try
{
Session mySession = sessionFactory.getCurrentSession();
mySession.save(message);
mySession.flush();
} catch (Exception e)
{
log.fatal(e.getMessage());
}
}


}

这是我的主要类(class):

public static void main(String[] args)
{
ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);


MessageService mService = context.getBean(MessageService.class);

HelloWorld helloWorld = context.getBean(HelloWorld.class);

/**
* Date: 4/26/13 / 9:26 AM
* Comments:
*
* I added Log4J to the example.
*/

LOGGER.debug("Message from HelloWorld Bean: " + helloWorld.getMessage());

Message message = new Message();
message.setMessage(helloWorld.getMessage());
//
mService.SaveMessage(message);



helloWorld.setMessage("I am in Staten Island, New York");


LOGGER.debug("Message from HelloWorld Bean: " + helloWorld.getMessage());
}
}

这是我的数据库配置:

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 + '\'' +
'}';
}
}

最佳答案

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

关于java.sql.SQLException : This function is not supported using HSQL and Spring 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16241032/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com