【问题标题】:How to connect with two database instances?如何连接两个数据库实例?
【发布时间】:2014-03-07 09:10:25
【问题描述】:

无法从多对一关系中获得结果
我有两个数据库:

@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.

【问题讨论】:

  • 我没有看到任何问号。
  • 我们有两个数据库。多对一关系没有得到结果。如何隐式连接另一个数据库
  • @user3386628 请编辑您的问题/帖子,而不是编写 cmets。
  • 检查您的管理员数据库配置?它会抛出什么异常?

标签: java spring hibernate jpa


【解决方案1】:

创建两个@PersistenceContext,它们有2个不同的数据库配置。

【讨论】:

    【解决方案2】:

    我对上一个问题的回答提供了代码示例,演示了如何做到这一点:

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

    【讨论】:

      猜你喜欢
      • 2018-03-27
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多