Reputation: 3473
I was facing problem in hibernate mappings with onetomany Bidirectional.
Whenever I run application it automatically creates two columns in Primary key table as first_name and last_name.
I dont know what exact mistake. here is my code that mappings in springboot. Sorry, I m in beginner level in spring.
TABLE persons :
@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;
...................
.............
}
My application.properties file:
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
My MySql query to create table:
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)
);
So my data at first is
When I run Springboot application It automatically creates first_name and last_name by alterting table.
My logcat is:
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)
My table after running project:
Upvotes: 0
Views: 1992
Reputation: 2665
By default, Spring Boot configures the physical naming strategy with SpringPhysicalNamingStrategy
. Which does this: For example, a TelephoneNumber
entity is mapped to the telephone_number
table (same goes for columns).
As you have used spring.jpa.hibernate.ddl-auto=update
, Spring will update the table adding new columns. In this case first_name
for FirstName
and last_name
for LastName
.
If you prefer to use Hibernate 5’s default instead, set the following property:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Read Configure Hibernate Naming Strategy in Spring Boot Reference for details.
Upvotes: 2