Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

how to use Hibernate for two different schemas in a single database

I want to use two different schema in database, each schema has same set of Tables but data differs. How to use hibernate and point to two different schema.I am new to the hibernate.Please provide me the links.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can specify it by schema element while defining table for your entity.

@Table(name="TABLE_NAME", schema="SCHEMA_NAME")

Else, you can use separate EntityManager pointing to respective schema & then use the same entity, as their structure is similar.


Edit : You can have separate configuration files for each schema & then build SessionFactory from it, below is some pseudo-code for it.

SessionFactory sf_1 = new  Configuration().configure("schema1config.cfg.xml").buildSessionFactory();
SessionFactory sf_2 = new Configuration().configure("schema2config.cfg.xml").buildSessionFactory();

session_1 = sf_1.openSession();  //-- Similarly for other

You can refer this link for further details to map multiple schema, but it isn't hibernate specific.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...