Я пытаюсь создать двунаправленную связь один к одному между двумя таблицами. Вот мои таблицы:
package com.dietsite.backend.admin.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
@Data
@Entity(name="UserEntity")
@Table(name = "USERS")
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
Long id;
@Column(unique = true, name="EMAIL")
private String email;
@Column(name = "NAME")
private String name;
@Column(name = "PASSWORD")
private String password;
@Column(name = "MOBILE")
private String mobile;
@Column(name = "OWNER_ID")
private String ownerId;
@Column(name = "TRAINER_ID")
private String trainerId;
@Column(name = "ADDED_ON")
private Timestamp addedOn;
@Column(name = "MODIFIED_ON")
private Timestamp modifiedOn;
@Column(name = "ADDED_BY")
private String addedBy;
@Column(name = "ADDED_BY")
private String modifiedBy;
@ElementCollection(fetch = FetchType.EAGER)
List<Role> roles;
@OneToOne(
mappedBy = "user",
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY,
optional = false
)
private UserStatisticsEntity userStatisticsEntity;
public void addUserStatisticsEntity(UserStatisticsEntity userStatisticsEntity) {
userStatisticsEntity.setUser( this );
this.userStatisticsEntity = userStatisticsEntity;
}
public void removeDetails() {
if ( userStatisticsEntity != null ) {
userStatisticsEntity.setUser( null );
this.userStatisticsEntity = null;
}
}
}
Дочерняя таблица:
package com.dietsite.backend.admin.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
@Data
@Builder(toBuilder = true)
@Entity(name = "UserStatisticsEntity")
@Table(name="USER_DETAILS")
@AllArgsConstructor
@NoArgsConstructor
public class UserStatisticsEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Column(name = "HEIGHT")
private Double height;
@Column(name = "WEIGHT")
private Double weight;
@Column(name = "GOAL_WEIGHT")
private Double goalWeight;
@Column(name = "FITNESS_EXPERIENCE")
private FitnessExperience fitnessExperience;
@Column(name = "DIET_PREFERENCE")
private DietPreference dietPreference;
@Column(name = "ACTIVITY_LEVEL")
private ActivityLevel activityLevel;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID")
private UserEntity user;
}
При попытке запустить программу я получаю следующую ошибку:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.dietsite.backend.admin.entity.UserEntity column: added_by (should be mapped with insert="false" update="false")
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1771) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.14.RELEASE.jar:2.1.14.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) ~[spring-boot-2.1.14.RELEASE.jar:2.1.14.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) ~[spring-boot-2.1.14.RELEASE.jar:2.1.14.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-2.1.14.RELEASE.jar:2.1.14.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.1.14.RELEASE.jar:2.1.14.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) ~[spring-boot-2.1.14.RELEASE.jar:2.1.14.RELEASE]
at com.dietsite.backend.DietSiteServerApplication.main(DietSiteServerApplication.java:20) ~[classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.dietsite.backend.admin.entity.UserEntity column: added_by (should be mapped with insert="false" update="false")
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1830) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1767) ~[spring-beans-5.1.15.RELEASE.jar:5.1.15.RELEASE]
... 16 common frames omitted
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.dietsite.backend.admin.entity.UserEntity column: added_by (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:862) ~[hibernate-core-5.3.17.Final.jar:5.3.17.Final]
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:880) ~[hibernate-core-5.3.17.Final.jar:5.3.17.Final]
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:902) ~[hibernate-core-5.3.17.Final.jar:5.3.17.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:634) ~[hibernate-core-5.3.17.Final.jar:5.3.17.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.3.17.Final.jar:5.3.17.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:347) ~[hibernate-core-5.3.17.Final.jar:5.3.17.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:466) ~[hibernate-core-5.3.17.Final.jar:5.3.17.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1250) ~[hibernate-core-5.3.17.Final.jar:5.3.17.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.15.RELEASE.jar:5.1.15.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.1.15.RELEASE.jar:5.1.15.RELEASE]
... 20 common frames omitted
Process finished with exit code 1
Я пробовал просмотреть множество статей об устранении проблемы и даже сослался на документацию по Hibernate. Текущий код - это то, как Hibernate упомянул здесь:
https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#associations
Что мне здесь не хватает? Помощь была бы признательна.