我在使用onetomany双向的休眠映射中遇到了问题。
每当我运行应用程序时,它都会在主键表中自动创建两个列,分别为first_name和last_name。
我不知道确切的错误。这是我在springboot中映射的代码。抱歉,我是春季初学者。
表格人员:
@Entity
@Table(name = "persons")
public class Persons {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "personid")
int PersonID;
@Column(name = "LastName")
String LastName;
@Column(name = "FirstName")
String FirstName;
@Column(name = "Address")
String Address;
@Column(name = "City")
String City;
@OneToMany(orphanRemoval = true, mappedBy = "persons", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
List<Person_detail> person_detail = new ArrayList<Person_detail>();
...................
..................
}
TABLE person_detail:
@Entity
@Table(name = "person_detail")
public class Person_detail {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column(name = "Address")
String Address;
@Column(name = "Designation")
String Designation;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "person_id", referencedColumnName = "personid")
Persons persons;
...................
.............
}
我的application.properties文件:
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/testapp
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/socket
spring.datasource.username=root
spring.datasource.password=roor
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
使用MySql查询创建表:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
CREATE TABLE Person_detail (
id int PRIMARY key AUTO_INCREMENT,
Address varchar(255),
Designation varchar(50),
person_id int,
FOREIGN KEY (person_id) REFERENCES persons(personid)
);
所以我的数据起初是
当我运行Springboot应用程序时,它会通过更改表自动创建名字和姓氏。
我的logcat是:
2018-06-25 20:35:15.119 INFO 6576 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Hibernate: create table hibernate_sequence (next_val bigint) engine=MyISAM
Hibernate: insert into hibernate_sequence values ( 1 )
Hibernate: alter table socket.persons add column first_name varchar(255)
Hibernate: alter table socket.persons add column last_name varchar(255)
答案 0 :(得分:2)
默认情况下,Spring Boot使用SpringPhysicalNamingStrategy
配置物理命名策略。这样做的目的是:例如,将TelephoneNumber
实体映射到telephone_number
表(列也是如此)。
使用spring.jpa.hibernate.ddl-auto=update
时,Spring将更新该表并添加新列。在这种情况下,first_name
的{{1}}和FirstName
的{{1}}。
如果您想改用Hibernate 5的默认设置,请设置以下属性:
last_name
阅读Configure Hibernate Naming Strategy in Spring Boot Reference了解详情。