avoid loading hbm files automatically - spring

I have two databases (Oracle and SqlServer) and I have the same table (Province) in both. I'm using JPA with Hibernate and need to perform operations to any of those databases.
I have an hbm mapping file for each table but both refer to the same class (Province.java).
I've configured hibernate.Oracle.cfg.xml and hibernate.SqlServer.cfg.xml in order to declare the mappings appropriately for each DB.
I'm also using Spring.
Here's my problem. When the application loads (actually JUnit) it tries to load both hbm files throwing DuplicateMappingException because Province.java is twice. Moreover, if I only keep one hbm file and one hibernate config file it still throws DuplicateMappingException; BUT if I remove the mapping from the hibernate config file then it works (but I don't want this). This means that the hbm file is automatically loaded and it is then loaded again due to it is declared in the config file.
Is there a way I can avoid hbm files to be automatically loaded?

I was finally able to find the solution to this problem. I added <exclude-unlisted-classes>false</exclude-unlisted-classes> to both persistence units in persistence.xml file and it prevented the hbm files to be loaded automatically. I also had to place the mapping in both hibernate.Oracle.cfg.xml and hibernate.SqlServer.cfg.xml.

Related

Externalizing configuration for Hibernate Search

I am running hibernate search with spring boot. I have written a working configuration for my application. How ever, i want to externalize my configuration and use ./config/hibernate.properties instead of src/main/resources/hibernate.properties. After copying my properties file to the desired location, i am getting and exception:
nested exception is java.io.FileNotFoundException: class path resource [hibernate.properties] cannot be opened because it does not exist
Anyone with any idea on how i should tell spring to read my configuration file?
Move your configuration to an src/main/resources/application.properties file and prepend spring.jpa.properties. everywhere, so hibernate.dialect will become spring.jpa.properties.hibernate.dialect, for example.
Then you can use Spring features to move your configuration wherever you want. To move it to ./config/application.properties I suppose you will have to add #PropertySource("./config/application.properties") to one of your #Configuration classes, or something similar.
I'm sure you can also keep the hibernate configuration in a separate file (separate from the rest of your application configuration).
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details about externalizing configuration in Spring Boot.
For some reason, it seems hibernate-search will prevent application from starting as long as a hibernate.properties configuration file does not exist. After trying for a while without success, i found a work around for my problem.
First, i created an empty hibernate.properties file and place it under src/main/resources.
Secondly, i moved all hibernate-search configurations to application.properties as follows:
spring.jpa.properties.hibernate.search.default.indexmanager = elasticsearch
spring.jpa.properties.hibernate.search.default.elasticsearch.host = http://my-server.com
spring.jpa.properties.hibernate.search.default.elasticsearch.index_schema_management_strategy = CREATE
spring.jpa.properties.hibernate.search.default.elasticsearch.required_index_status = yellow
This way, the application will start and spring will get all configuration from the externalized configuration as documented here.

Spring with Hibernate- how to pass schema name dynamically

I am using spring-Hibernate DAO Layer, i have created spring configuration file having data source and all the hbm xml list and each hbm is mapped with the table as
table ="schema1.table1"
We have another schema say prodSchema in production, i want to know how to pass schema name dynamically by using propfile etc.to the hbm file so that in runtime i can change.
Java annotation only supports compile time constants. i.e. you cannot put variable in annotation parameter.
REf : http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28
Your situation is common to most of the project and developer. I generally use external database configuration file, where I put the connection parameters, schema and credentials. This file is created on each environment once and remains unchanged until there is any change in DB server.
General Hibernate Property for quick reference.
hibernate.connection.driver_class : JDBC driver class
hibernate.connection.url : JDBC URL (may contain schema)
hibernate.connection.username : database user
hibernate.connection.password : database user password
hibernate.default_schema : Qualify unqualified table names with the
given schema/tablespace in generated SQL. e.g. SCHEMA_NAME (use this
if you don't specify in JDBC URL)

Any way to create DB(derby) in Spring MVC Project?

I am working on a program which uses memory DB(in my case, it is apache derby, is included in jdk 1.6 and further).
I can set resources like dataSource and sqlSession bean to 'root-context.xml', but do not know how to create DB and table.
If table and DB are already exist, I can do CRUD via usual way. However, this time I should make a DB and new Table on first time. I already tried Maker Class and put 'init-method' to create DB and table before was is uploaded, but it does not work.
How can I make a Spring was program which makes Memory DB and Table before running?
P.S.
I really like to use CRUD in Spring way like SqlSessionTemplate or annotation. But almost of derby sample uses PreparedStatement or Statement. If you have any good sample links what I am looking for, share please. Thanks :D
You need to use the jdbc namespace which includes an embedded-database tag with support for Derby out of the box. The tag takes nested script tags to define scripts to run (they are run in the order declared).
<jdbc:embedded-database id="myDB" type="DERBY" >
<jdbc:script location="classpath:sql/schema.sql"/>
<jdbc:script location="classpath:sql/data.sql"/>
</jdbc:embedded-database>

Is it webapp-root/META-INF or webapp-root/WEB-INF/classes/META-INF (Tomcat 7)

Unless I put META-INF in the root, context.xml along with the resources defined (queues, jdbc connection pool, etc) are not read correctly and Spring throws exceptions during start up.
Now I end up with two META-INF folders, I need one on the classpath (WEB-INF/classes/META-INF) due to persistence.xml.
Is is okay to have two META-INF's? I always configured resources in Spring's applicationContext so didn't notice before.
Thanks.
I guess it is all right. They both might have slightly different purposes.
webapp-root/META-INF is directory for metadata of the web application, which you can access via servletContext.getResource("/META-INF/foo").
webapp-root/WEB-INF/classes/META-INF is directory for metadata of java classes, which you can read via getContextClassLoader().getResource("/META-INF/foo").

Spring application-context, not able to load (property-placeholder) .properties file

I have a web-app, which loads a application-context files from many locations.
One of the application-context file is in a .jar file (this jar is present in WEB-INF/lib).
This application-context has an entry like this:
<context:property-placeholder location="classpath:META-INF/spring/default.app.properties" ignore-unresolvable="true" ignore-resource-not-found="true"/>
But the default.app.properties is never found. I keep getting errors about
Could not resolve placeholder 'db.driver' - something that is defined in default.app.properties and referred in application-context via ${db.driver}
It is almost as if property-placeholder is being ignored. I tried giving absolute path to my default.app.properties too.. even that wouldn't work.
Have you solved this problem? I've encoutered the same recently. My solution is simple and unlikely to be the case but... in my case there were two types of placeholders of different types. One type was configured using
<context:property-placeholder/>
the other type was configured as a bean of type ServletContextPropertyPlaceholderConfigurer. Removing one type of placeholder solved the problem.
Because Spring allow exist only one <context:property-placeholder/>, When Spring find a <context:property-placeholder/>,it will ignore the remains. So put all the properties conf in one place.
reference to :http://www.iteye.com/topic/1131688

Resources