我第一次有使用Spring MVC的经验,当我尝试将值存储在PostgreSQL中时,我遇到了错误。我正在使用id的序列类型。
" HTTP状态500 - 请求处理失败;嵌套异常是org.springframework.dao.InvalidDataAccessResourceUsageException:无法获取下一个序列值; SQL [select nextval(' CUSTOMER_SEQ')];嵌套异常是org.hibernate.exception.SQLGrammarException:无法获得下一个序列值"
这是我的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="java.mvc.domains.Customer" table="CUSTOMER">
<id name="id" column="ID" type="integer">
<generator class="sequence">
<param name="sequence">CUSTOMER_SEQ</param>
</generator>
</id>
<property name="name" column="NAME" type="string"></property>
<property name="email" column="EMAIL" type="string"></property>
<property name="mobile" column="MOBILE" type="string"></property>
</class>
</hibernate-mapping>
配置文件:
.....
<bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</prop>
</props>
</property>
<property name="mappingResources">
<array>
<value>Customer.hbm.xml</value>
</array>
</property>
</bean>
......
这是POJO:
public class Customer {
private int id;
private String name;
private String email;
private String mobile;
// Getters and setters
这是控制器类:
public class CustomerController implements Controller {
@Autowired
private HibernateTemplate hibernateTemplate;
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{
String name = request.getParameter("name").trim();
String email = request.getParameter("email").trim();
String mobile = request.getParameter("mobile").trim();
Customer customer = new Customer();
customer.setName(name);
customer.setEmail(email);
customer.setMobile(mobile);
Integer id = (Integer) hibernateTemplate.save(customer);
return new ModelAndView("Customer", "id", id);
}
}
错误的屏幕截图:
但它适用于&#34;已分配&#34;型
答案 0 :(得分:0)
Hibernate的PostgreSQL方言并不是很明亮。它并不知道你的每个SERIAL序列,并假设有一个全球数据库范围的序列叫做#34; hibernate_sequence&#34;它可以使用。
似乎较新的Hibernate版本可能在GenerationType时使用默认的每表序列。 IDENTITY已指定。并检查CUSTOMER_SEQ
是否正确创建