【发布时间】:2020-01-10 11:31:40
【问题描述】:
我正在使用 -
spring.jpa.hibernate.ddl-auto=create(我正在使用 spring boot jpa 项目)。
因此,hibernate 正在删除早期的表并按预期重新创建新的表。在本帖中,区分hibernate output
和
java code
我正在使用上述约定。
Hibernate 正在查询并尝试更改现有架构,如下所示 -
alter table state
drop
foreign key FKcdpxn6x9xj5h0r44m8poebva0
...等等
drop table if exists state
在删除约束和表格之后 - 它正在重新创建表格和约束..
2019-09-08 13:14:05.794 DEBUG 5016 --- [ restartedMain] org.hibernate.SQL :
create table state (
name varchar(255) not null,
population varchar(255),
country_name varchar(255),
primary key (name)
)
...创建更多的表,然后是约束...
2019-09-08 13:14:08.355 DEBUG 5016 --- [ restartedMain] org.hibernate.SQL :
alter table state
add constraint FKcdpxn6x9xj5h0r44m8poebva0
foreign key (country_name)
references country (name)
到目前为止一切顺利...接下来是java代码中的插入操作...
Country usa = new Country ("USA", "330000000"); countryRepository.save(usa);
我的问题就在这里。在插入之前,hibernate正在做一个select语句...
2019-09-08 13:14:10.915 DEBUG 5016 --- [ restartedMain] org.hibernate.SQL :
select
country0_.name as name1_1_1_,
country0_.population as populati2_1_1_,
states1_.country_name as country_3_2_3_,
states1_.name as name1_2_3_,
states1_.name as name1_2_0_,
states1_.country_name as country_3_2_0_,
states1_.population as populati2_2_0_
from
country country0_
left outer join
state states1_
on country0_.name=states1_.country_name
where
country0_.name=?
2019-09-08 13:14:10.915 TRACE 5016 --- [ restartedMain] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [USA]
下一个 Hibernate 正在执行实际的插入...
2019-09-08 13:14:11.552 DEBUG 5016 --- [ restartedMain] org.hibernate.SQL :
insert
into
country
(population, name)
values
(?, ?)
2019-09-08 13:14:11.553 TRACE 5016 --- [ restartedMain] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [330000000]
所以,我想知道在插入之前查询表的目的是什么。
更新/编辑 - 按照 cmets 的要求,java 模型代码如下。
@Table(name="state")
@Entity
public class State {
@Id
@Column(name="name")
String name;
@Column(name="population")
String population;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="state_name")
List<City> cities;
@ManyToOne (cascade=CascadeType.ALL, fetch=FetchType.LAZY)
Country country;
getters / setters
@Table(name="country")
@Entity
public class Country {
@Id
@Column(name="name")
String name;
@Column(name="population")
String population;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="country_name")
List<State> states;
getters/setters
【问题讨论】:
-
猜测:检查值是否已经存在以避免违反主键的唯一性
-
是的,但是插入无论如何都会失败...那何必呢。接下来,为什么要查询指向
country表的表。查询state表有什么意义。 -
您没有提供任何 JAVA 代码。我们无法看到您的对象是如何映射到表格的。
-
如果是更新,那么查询
state是有意义的,因为它必须确保现有关系不会中断。 -
@LukaszSzozda,型号代码已更新
标签: java mysql hibernate orm spring-data-jpa