简体   繁体   English

是否可以使用单个hibernate会话将对象保存在两个不同的模式中?

[英]Is it possible to save object in two different schemas using single hibernate session?

I am trying to persist person object into two different schemas using single hibernate session as follows 我试图使用单个hibernate会话将person对象持久化为两个不同的模式,如下所示

I have Person Class having certain fields in it 我有Person Class,里面有某些字段

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Integer id;
    @Column(name="firstname")
    private String firstName;
    @Column(name="lastname")
    private String lastName;
    @Column(name="age")
    private int age;

//getters and setters methods

Now I need to save the object into two different schemas using single hibernate session. 现在我需要使用单个hibernate会话将对象保存到两个不同的模式中。 Can this happens, can anybody have any idea please help me. 这可能发生,任何人都有任何想法请帮助我。

I have answered my question 我已经回答了我的问题

Its not possible using same session for persist objects into two different schemas. 它不可能将持久化对象的相同会话用于两个不同的模式。

I have done like below: 我做了如下:

I have create two hibernate configuration files for the two different schemas you need to persist the object. 我为持久化对象所需的两个不同模式创建了两个hibernate配置文件。

Here is the code snippet: 这是代码片段:

Configuration config1 = new Configuration().configure(configfile1);
Configuration config2 = new Configuration().configure(configfile2);

I have opened the sessionfactory's by using both configuration instances 我已经使用两个配置实例打开了sessionfactory

SessionFactory sessionFactory1 = config1.buildSessionFactory(); 
SessionFactory sessionFactory2 = config2.buildSessionFactory();

I have opened two new sessions from sessionFactory instances 我从sessionFactory实例中打开了两个新会话

Session session1 = sessionFactory1.openSession();
Session session2 = sessionFactory2.openSession();

Now I have persisted the objects to two different schemas. 现在我将对象持久化为两个不同的模式。

But Not sure whether this is correct way to handle. 但不确定这是否是正确的处理方式。

Thanks 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在休眠的单个会话中执行两个save(“ entity-name”,object)? - Perform two save(“entity-name”,object) in a single session of hibernate? Hibernate:在同一个应用程序中使用两个不同的DataBase模式 - Hibernate: Using two different DataBase schemas in the same application Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save - Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save hibernate session.save()插入与对象不同的值 - hibernate session.save() inserts different values than are at object 在两个不同的模式中使用两个具有相同名称的表时,避免使用Hibernate Annotation Exception - Avoiding Hibernate Annotation Exception when using two identically named tables in two different schemas Spring Boot使用不同的命名约定休眠以进行会话保存 - Spring Boot hibernate using different naming convention to session save Java Hibernate将对象保存在一个数据库中的不同模式中 - Java hibernate save objects in different schemas which are in one database 休眠:在另一个会话中更新对象? - Hibernate: updating an object in a different Session? 使用session.save保存对象时,Hibernate不会刷新会话? - Hibernate does not flush session when save object using session.save? 是否可以将同一对象保存到两个不同的表Room中? - If is it possible save the same object to two different table Room?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM