-1

Unable to get results from many to one relation
I have two database:

@PersistenceContext(unitName = "admin")
    public void setAdminEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

@PersistenceContext(unitName = "user")
    public void setUserEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

----------------------------------------------

@Entity
@Table(name = "cd_state")
@NamedQueries({ 
    @NamedQuery(name = "State.findAll", query = "Select s from State s order by s.stateName ASC "),  

})
public class State {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long stateId;
    private String stateCode;
    private String stateName;

    @ManyToOne
    @JoinColumn(name = "countryId", insertable = false, updatable = false)
    private Country country;
    private Boolean isActive;
    private Long countryId;
getters and setters//
}

country belongs to admin databases::
-----------------------------

public List<User> getAllUsers() {
        TypedQuery<User> query = getuserEntityManager().createNamedQuery(FIND_ALL_USERS, User.class);

        List<User> list = query.getResultList();

        return list;
    }

it throws exception.not connecting to admin database.
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
stephen
  • 52
  • 1
  • 8

2 Answers2

0

Create two @PersistenceContext which has 2 different database configurations.

Jay
  • 9,189
  • 12
  • 56
  • 96
0

This answer I provided to a previous question provides code examples demonstrating how to do it:

Multiple jpa:repositories in xml config, how to configure with @EnableJPARepositories using Spring java config?

Community
  • 1
  • 1
Steve
  • 9,270
  • 5
  • 47
  • 61