0

I created the following table within the catalog "data" that requires to reference an entity called "UserAccount" in a different catalog namely, "useraccount". So I write:

@Entity
@Table(name = "data", catalog = "data")
public class Data implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, unique = true)
private String name;

@ManyToOne(targetEntity = UserAccount.class)
@PrimaryKeyJoinColumn
private UserAccount user;
....//
}

But using "targetEntity" parameter seems not working since I get the following error:

@OneToOne or @ManyToOne on [cut].Data.user references an unknown entity: [cut].user.UserAccount

Hence, the reference is not right. How to correctly refer to entities from another catalog?

Edit. For catalog I mean Database schema:

enter image description here

Edit: One of the configuration file

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "userEntityManager",
    transactionManagerRef = "userTransactionManager",
    basePackages = {"it.unict.spring.platform.persistence.repository.user"}
    )
public class JpaTransactUser
{

@Bean(name = "userEntityManager")   
public LocalContainerEntityManagerFactoryBean getServersEntityManager(
                EntityManagerFactoryBuilder builder,
                @Qualifier("userSource") DataSource serversDataSource){
    return builder
            .dataSource(serversDataSource)
            .packages("it.unict.spring.platform.persistence.model.user")
            .persistenceUnit("userUnit")
            .properties(additionalJpaProperties())
            .build();

}

Map<String,?> additionalJpaProperties()
{
    Map<String,String> map = new HashMap<>();

    map.put("hibernate.hbm2ddl.auto", "update");        
    //map.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    map.put("hibernate.show_sql", "true");
    return map;
}


@Bean("userDataSourceProperties")    
@ConfigurationProperties("app.datasource.user")
public DataSourceProperties dataDataSourceProperties()
{
    return new DataSourceProperties();
}


@Bean("userSource")    
@ConfigurationProperties("app.datasource.user")
public DataSource getDataSource(@Qualifier("userDataSourceProperties") DataSourceProperties dataDataSourceProperties) {
    return dataDataSourceProperties().initializeDataSourceBuilder().build();
}

@Bean(name = "userTransactionManager")
public JpaTransactionManager transactionManager(@Qualifier("userEntityManager") EntityManagerFactory serversEntityManager)
{
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(serversEntityManager);
    return transactionManager;
}

}

Discipulos
  • 423
  • 8
  • 23
  • What do you mean by catalog? Java Package? Are you sure all your entities are picked up by JPA? – TomStroemer Aug 01 '22 at 10:07
  • I mean "schema". I modified the question. – Discipulos Aug 01 '22 at 10:13
  • @Discipulos, can you share your code where you have used `@EnableJpaRepositories` – Ashish Patil Aug 01 '22 at 10:46
  • I avoid to modify the question and prefer a pastebin: https://pastebin.com/p3dHhBG8 I have two configuration, one for each database. – Discipulos Aug 01 '22 at 11:17
  • Please, edit your question to include relevant source code instead of linking to external sources. – Jens Schauder Aug 01 '22 at 12:34
  • @JensSchauder The request of adding the code that I linked is not related with the question, hence I prefered an external link – Discipulos Aug 01 '22 at 12:40
  • The error message says "unknown entity". Normaly this happens, when JPA doesn't know the class. Errors with the schema will probably appear at runtime, when you try to access the table (select, update, ...). So this code is relevant and should be added to the question. Thanks. See also this question: https://stackoverflow.com/questions/55697586/unknown-entity-when-calling-entitymanager-persist – TomStroemer Aug 01 '22 at 15:06
  • I added the code in the question. Note that the error comes at compile time. The problem is that I don't know how to tell JPA how to find the UserAccount's schema. – Discipulos Aug 01 '22 at 15:25

0 Answers0