jpa设置ID为默认的@GeneratedValue,不能生存auto_increatement的表,导致如果原表有内容,就出错,说主键重复。这是怎么回事呢?
hibernate 5.1.0

我的实体类:
@Table(name = "sssp_empoyee")
@Entity
public class Employee {
private Integer id;
private String lastName;
private String email;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birth;
private Date crateTime;
private Department department;
@GeneratedValue
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
我的spring设置:
<property name="jpaProperties">
<props>
<prop key="hibernate.implicit_naming_strategy">legacy-jpa</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
</props>
</property>
<property name="sharedCacheMode" value="ENABLE_SELECTIVE"/>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我也遇到了这个问题,应该是看了尚硅谷的视频吧。
详情查看: https://hibernate.atlassian.n...
解决方法:
将
hibernate.id.new_generator_mappings设置为false