我在使用onetomany的hibernateMap时遇到了问题。
每当我运行应用程序时,它都会自动在主键表中创建两列作为first\u name和last\u name。
我不知道到底是什么错误。这是我在springboot中Map的代码。对不起,我在 Spring 入门。
餐桌人员:
@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>();
...................
..................
}
表格详情:
@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应用程序时,它通过改变表自动创建名和姓。
我的目标是:
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)
运行项目后的我的表:
1条答案
按热度按时间p4tfgftt1#
默认情况下,spring boot使用
SpringPhysicalNamingStrategy
. 它是这样做的:例如TelephoneNumber
实体Map到telephone_number
表(列也是如此)。就像你用的那样
spring.jpa.hibernate.ddl-auto=update
,spring将更新表并添加新列。在这种情况下first_name
为了FirstName
以及last_name
为了LastName
.如果您更喜欢使用hibernate 5的默认值,请设置以下属性:
有关详细信息,请参阅spring boot reference中的configure hibernate naming strategy。