Reputation: 4719
I am making a basic CURD Spring boot. I generated the Spring Boot app from https://start.spring.io/ with web, jpa, and mysql references.
As usual, I defined the Repository as follows:-
public interface URepository extends JpaRepository<SharedKeyUser, Long> {
}
I got this error:-
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'URepository' defined in com.example.sharedkeyonetoone.repository.URepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to asynchronously initialize native EntityManagerFactory: java.util.NoSuchElementException
Initially, I had UserRepository and later I renamed to URepository. I am getting the same error all the time. What is wrong?
More class as asked:-
public interface ARepository
extends JpaRepository<SharedKeyAddress,Long> {
}
@Entity
@Table(name = "shared_key_address")
public class SharedKeyAddress {
@Id
@Column(name = "id")
private Long id;
@Column(name = "street")
private String street;
@Column(name = "city")
private String city;
@OneToOne
@MapsId
private SharedKeyUser user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public SharedKeyUser getUser() {
return user;
}
public void setUser(SharedKeyUser user) {
this.user = user;
}
}
@Entity
@Table(name = "shared_key_users")
public class SharedKeyUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "username")
private String userName;
@OneToOne(mappedBy = "sharedKeyUser", cascade = CascadeType.ALL)
private SharedKeyAddress address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public SharedKeyAddress getAddress() {
return address;
}
public void setAddress(SharedKeyAddress address) {
this.address = address;
}
}
Also applicaiton.properties:-
spring.datasource.url=jdbc:mysql://localhost:3306/my_dev?
useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username=dev
spring.datasource.password=dev
# Hibernate
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
log4j.category.org.springframework.web=INFO
logging.level.org.hibernate.SQL=DEBUG
I am trying to make one to one relation based on a shared primary key.
Upvotes: 2
Views: 6233
Reputation: 1278
A lot can be changed to your code. First, put @Repository annotation to your repositories. Second remove the mappedBy = "sharedKeyUser"in SharedKeyUser. and put @JoinColumn(name = "address_id", referencedColumnName = "id"). Put your mappedBy in your address class instead.
@Repository
public interface ARepository
extends JpaRepository<SharedKeyAddress,Long> {
}
@Entity
@Table(name = "shared_key_address")
public class SharedKeyAddress {
@Id
@Column(name = "id")
private Long id;
@Column(name = "street")
private String street;
@Column(name = "city")
private String city;
@OneToOne(mappedBy = "address")
private SharedKeyUser user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public SharedKeyUser getUser() {
return user;
}
public void setUser(SharedKeyUser user) {
this.user = user;
}
}
@Entity
@Table(name = "shared_key_users")
public class SharedKeyUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "username")
private String userName;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id", referencedColumnName = "id")
private SharedKeyAddress address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public SharedKeyAddress getAddress() {
return address;
}
public void setAddress(SharedKeyAddress address) {
this.address = address;
}
}
Upvotes: 0
Reputation: 31
Repository name is not a problem. Make sure you annotated the SharedKeyUser Pojo with correct Annotations. @Entity, @ID and @GeneratedValue from javax.persistence API. And make sure you also set the data source properties for the application.
Upvotes: 1