Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark a persistence-unit as default in JPA (hibernate)?

I read a few threads on setting a one out of n persistence-unit in JPA as default. But the usage of hibernate.default_schema seems to be vague.

However, I want to achieve the following.

  • Have one default <persistence-unit name="myMainPU"> ... </persistence-unit>
  • Have one or more additional <persistence-unit name="auxilliaryPU"> ... </persistence-unit>
  • I don't want to annotate my injected EntityManagers with the name attribute. E.g. not like this @Persistence(unitName="myMainPU") for the default persistence unit. Should be simply like this @Persistence.
  • I want to annotate my auxilliary PUs with the unitName attribute. E.g. @Persistence(unitName="auxilliaryPU")

How does one achive that? I use Hibernate 4+ and could also accept a proprietary solution.

Thanks.

like image 332
feder Avatar asked Dec 31 '25 02:12

feder


1 Answers

There is no such default option of "default pu".

What you could do is to use CDI and decide which PU you will use. I cannot see other way of doing it.

like image 171
uaiHebert Avatar answered Jan 02 '26 15:01

uaiHebert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!