0

I am trying to have a OneToMany and ManyToOne mapping in my persistence classes. I have following tables:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "file")
public class File {
    @Id
    @GeneratedValue(strategy = AUTO)
    private Long fileId;
    @NotBlank(message = "File Name not empty or null")
    private String fileName;
    private Instant createdDate;
    private Long customerId;
    @ManyToOne(fetch = FetchType.EAGER, targetEntity = Customer.class)
    @Column(name = "files")
    private Customer customer; 
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "customerDB")
public class Customer {
    @Id
    @GeneratedValue(strategy = AUTO)
    private Long id;
    @NotBlank(message = "Customer name is not null")
    private String customerName;
    @NotBlank(message = "İnfo is not null")
    private String info;
    private Instant createdDate;
    private Long createdUser;


    @OneToMany(targetEntity = File.class, mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<File> files;

application.properties

app.datasource.file.url=jdbc:mysql://localhost:3306/filedb?createDatabaseIfNotExist=true
app.datasource.file.username=root
app.datasource.file.password=password
app.datasource.file.driverClassName=com.mysql.cj.jdbc.Driver

app.datasource.customer.url=jdbc:mysql://localhost:3306/customerdb?createDatabaseIfNotExist=true
app.datasource.customer.username=root
app.datasource.customer.password=password
app.datasource.customer.driverClassName=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect 

That is, I am getting errors from not trying many one-to-many operations with a lot of problems online. In version in one software. In applications with multiple versions. I couldn't find the solution.I get pretty much the same error with every solution I try. I tried many methods but the result is always an error.


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerEntityManagerFactory' defined in class path resource [com/nishbs/cas/configuration/CustomerSourceConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.nishbs.cas.model.customer.Customer.files[com.nishbs.cas.model.file.File]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.6.jar:2.6.6]

2 Answers 2

1

It seems that you are trying to join two tables that are in different databases. I am not 100% certain but i would say that is not possible the way you are doing it.

Even without spring as a framework you have to link your databases first. I would guess that if you have achieved this then you can access the Database with just one datasource in spring.

Start of with linking your databases and trying to run a manual SQl query, if this works the try again with code. I am not an expert on this but i would start with looking for something linke this Oracle Database Link - MySQL Equivalent?

Sign up to request clarification or add additional context in comments.

1 Comment

What you said is correct. There is no way to do this. But the link alternative solutions you shared do work. Thanks.
-1

Firstly be sure that for @Entity annotation you have imported this javax.persistence.Entity package.

When you are joining two tables there will be one more table created. So, I you should specify the name of new table while mapping and also provide the referencedColumnName.

@OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "post_id", referencedColumnName = "id")
    private List<Comment> comments;

Above is one such example which I am trying to say. Try to implement in this way.

1 Comment

The problem is not in the manytoone or onetomany definition. establishing the table relation in multidatabase. I can't solve that problem.When I perform the pojos I shared above using a single database, it works without any problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.