我能够成功地将数据插入到mysql数据库中,现在我将我的数据库切换到postgresql并且我正在接受这个挑战
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [admin_pkey]; nested exception is org.hibernate.exception.ConstraintViolationException:
could not execute statement
我已经为我的id列确保了generation_auto注释,但仍然存在错误,这是我的类映射
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(unique = true, nullable = false)
private String name;
@Column(unique = true, nullable = false)
private String email;
@Column(unique = false, nullable = false)
private String password;
@Column(unique = false, nullable = false)
private Date date;
public Integer getId() {
return id;
}
答案 0 :(得分:0)
如果要更改数据库,则应相应地更改hibernate配置XML
这是hibernate.cfg.xml
:
<!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.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection_pool_size">1</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping class="org.sample"/>
</session-factory>
</hibernate-configuration>