2

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.

masiboo
  • 4,537
  • 9
  • 75
  • 136

2 Answers2

1

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.

Gowtham
  • 31
  • 3
  • pls, check more code added. I am trying to make one to one relation based on a shared primary key. – masiboo Jul 14 '20 at 21:36
  • Use Generation Type as Identity in SharedKeyUser Pojo. – Gowtham Jul 15 '20 at 06:53
  • Sorry, I am not clear enough what do u meant by generation type as identity? – masiboo Jul 15 '20 at 07:07
  • Read [this](https://stackoverflow.com/a/8473985/10635467) Change the GenerationType.Auto to GenerationType.Identity Hope It will Work for shared primary key. – Gowtham Jul 15 '20 at 07:12
  • After updating to Identity. I am getting new error as:- Field 'id' doesn't have a default value 2020-07-15 12:26:53.775 ERROR 32142 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement] with root cause – masiboo Jul 15 '20 at 09:40
  • I visited the link for the tutorial you have followed for this project. It is very straightforward and one thing that can cause error will be the Generation type. I mentioned only that. Rather than everything will work as expected if you done everything correctly. Check for the Database schema and insertion of data whether you have made it right or not! – Gowtham Jul 17 '20 at 08:47
0

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;
    }
}
rhandom
  • 1,198
  • 3
  • 10
  • 15
  • 1
    This works and I have done in your suggested way. But it will be something like one to one relationship based on Foreign Key. But I want to make it using a Shared Primary Key. More at this blog https://www.baeldung.com/jpa-one-to-one. It has 3 kinds of one to one relationship. For me Using a Foreign Key works and Using a Join Table works. But Using a Shared Primary Key doesn't work. – masiboo Jul 15 '20 at 06:41
  • Okay sorry i didn't get the question. I think the error might be in your mappedBy.. the value should be "user" instead of "sharedKeyUser"... Change @OneToOne(mappedBy = "sharedKeyUser", cascade = CascadeType.ALL) to @OneToOne(mappedBy = "user", cascade = CascadeType.ALL) – rhandom Jul 15 '20 at 07:17