Spring boot application and hibernate are using different naming strategies - spring-boot

This is very strange.
I am using the spring.jpa.generate-ddl=true to create the tables in my database. When spring does this, the table names are snake case.
For example, the table for MyClass becomes my_class.
Next, I exported the schema from Hibernate using this code:
Export schema from Hibernate
When I call the code, the resulting sql has table names in camel case with the first letter capitalized. So, the table for MyClass is MyClass.
Why would they generate differently?
How do I get them to be the same?

The table names are different because Spring configures Hibernate to use a different naming strategy. The naming strategy defines how Hibernate generates the logical name of an attribute or entity and how to map them to the physical name in the database. I explain that in more detail in the Naming Strategy Guide on my blog.
You have multiple options to ensure both of your setups use the same naming strategy:
If you're using a Hibernate version >= 5.5.4, you can configure it to use the CamelCaseToUnderscoresNamingStrategy. That's the one that Spring is using by default:
<persistence>
<persistence-unit name="my-persistence-unit">
...
<properties>
...
<property name="hibernate.physical_naming_strategy"
value="org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy"/>
</properties>
</persistence-unit>
</persistence>
If you're using an older Hibernate version, you might want to change Spring's naming strategy to Hibernate's default strategy. You can do that by setting the following property in your application.properties file:
spring.jpa.properties.hibernate.implicit_naming_strategy=default
If you're using an older Hibernate version and want to use Spring's naming strategy, you have to implement a custom naming strategy. This isn't complex, and I explain how to do it in my blog post, but I would choose one of the previously mentioned solutions.

Related

hibernate entity-manager v/s hibernate jpa package which to use

How do I configure hibernate?
If hibernate-entitymanager package is used for JPA then what good is of hibernate-jpa package. And I have seen that they use org.hibernate.ejb.HibernatePersistence class is used for JPA provider in persistence.xml which is in hibernate-entitymanager package.
In some cases, I have not seen the provider tag in persistence.xml. In this case which class is used?. Does Session class has EntityManager or implements it . And none of hibernate beans are specified in Spring-application context file directly. Instead spring.orm packages are used. So spring framework picks which class for JPA functionality.
org.springframework.orm.jpa.LocalEntityManagerFactoryBean picks which bean if provider tag is specified and if provider tag is not specified which bean is picked?
Hibernate JPA is a standard JPA implementation. See also these other questions about differences.
What's the difference between JPA and Hibernate?
Difference between Hibernate and Hibernate JPA
similarity and difference between jpa and hibernate
Session and EntityManager do approximately the same thing. EntityManager is the "new way."
I'm not clear on what your questions are about the persistence.xml file.
Thanks for reply but my Question was in which package the jpa implementation actually is . Is it in hibernate entitymanager.jar or hibernate-jpa2.1.jar . I found this fallowing link to be of little use
.https://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/ch01s02s01.html
but in this case in persistence.xml file contains org.hibernate.ejb.HibernatePersistence as jpa provider but this class is in hibernate-entitymanager package . But they also told that hibernate-jpa-2.0-api.jar is the JAR containing the JPA 2.0 API, it provides all the interfaces and concrete classes that the specification defines as public API. Said otherwise, you can use this JAR to bootstrap any JPA provider implementation. in this fallowing link
https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html please could some one help me with this

How to Integrate Spring 4 with Hibernate Search 5?

I have already integrate Spring MVC+ Spring Framework 4 + Hibernate ORM 4.
Now I want to use full text search in hibernate search.
So, How to Integrate Spring Framework 4 with Hibernate Search 5?
According to the official documentation this should be as simple as:
Add Hibernate Search JAR to the application.
Add <property name="hibernate.search.default.directory_provider" value="filesystem"/> and <property name="hibernate.search.default.indexBase" value="/var/lucene/indexes"/> to the Hibernate configuration. Alternatively, you can choose a different provider.
Annotate the entities and fields you want to index.
If using the Hibernate Session directly, obtain a FullTextSession as Search.getFullTextSession(sessionFactory.getCurrentSession()), or if using an EntityManager, obtain a FullTextEntityManager as Search.getFullTextEntityManager(entityManager). It is assumed that sessionFactory or entityManager is injected using Spring.
Start searching. Entity instances are automatically indexed on persist so no special steps are required for generating the search indexes.
Give this a try and if you face any specific problem you can raise them as separate questions.

What is good practice to configure Spring MVC application with Spring security?

Assume I have Spring MVC powered application with Spring security. I have:
UserBean class which provides CRUD operations on table User
UserController : controller which expose operation on User to http clients
UserLogin: Authentication provider from Spring security, which authenticates users.
How should I configure my application if:
I want simple XML configuration, with auto-discovering beans by annotations (<context:component-scan base-package="org.example"/>)
UserLogin and UserController needs UserBean to work
UserLogin and UserController use transaction annotations and aspect annotations
I see the following oportunities:
Create one common Spring XML configuration file, used both by DispatcherServlet and ContextLoaderListener
Disadvantage: nobody shows that solution in tutorial. All beans are duplicated (one instance in ContextLoaderListener context, second in DispatcherServlet). Duplication may cause some hard to track bugs. Duplication is not elegant
Create two Spring XML configuration files, one for ContextLoaderListener (main) and one for DispatcherServlet (controllers). UserBean is declared in first config and visible in second one
Disadvantage: to avoid duplication I have to add complex component scanning rules to both files (context:component-scan). <tx:annotation-driven and <aop:aspectj-autoproxy/> must be defined in both files. I will have still doubts which config file is appropiate when declaring new stuff.
Create two Spring XML configuration files and include third for common settings like <tx:annotation-driven
Disadvantage: I wanted simple solution...
Summary: I'm looking for good practice to configure application with Spring MVC + Spring Security AND security part is highly connected with business part. I was searching for good example but I always find case when security code is isolated from business code. But I need example when security and business share the code
Similar question: ContextLoaderListener or not?
I have two xml files for my configuration, no particular reason, that's just how it worked out.
These sample spring security projects provide good examples of lots of different types of configurations maybe you can find something that works for you:
https://github.com/spring-projects/spring-security/tree/master/samples
Hidden message in my question was: having two contexts is stupid.
Did someone already notice that?
Is there a way to have single application configuration?
Answers:
Yes. https://jira.springsource.org/browse/SPR-6903
Yes. https://github.com/michaldo/spring-single-context-demo
The best practice which applies to my case is described here: https://stackoverflow.com/a/14032213/2365727

How do I change my configuration to a different data source?

I went through the Data Access With Spring tutorial and the in memory database they use in step 3 is working. But, I'm not clear on what I need to add/change to get it to query my development (Oracle) database now?
I want to use Hibernate, do I still need this JPAConfiguration class or would I have something Hibernate specific?
Please don't just post a link to the Hibernate reference. I'm reviewing that as well, but since I'm also using Spring, it's not clear to me the proper way to load the hibernate.cfg.xml and inject the Hibernate session in that context.
Don't be blocked by the fact that the class is called JPAConfiguration. You need to understand what the class does. Note that it has the annotation #Configuration which you can use along with AnnotationConfigApplicationContext to produce a Spring bean context.
That functionality is described in the Spring documentation for The IoC container.
What you need to change is how your DataSource and EntityManagerFactory beans are created. You'll need to use a DataSource that gets Connection instances from a JDBC Driver that supports Oracle databases.

Creating DB table through Entity Bean

I am working on EJB3. Is it possibe to create Oracle DB table at run time through Entity Bean?
For this i have created a entity bean and i have made entry for this bean to persistence.xml also. I thought it will create DB table at run time. But table was not created.
Is it possible to create table at run time through entity bean. If yes what i am doing wrong.
Thankx in advance for your time and help
You can configure entity generation in your persistence.xml, sample is shown below.
Make changes according to your environment.
<property name="toplink.ddl-generation"
value="drop-and-create-tables"/>
<!-- Generating derby specific sql -->
<property name="toplink.platform.class.name"
value="oracle.toplink.essentials.platform.database.DerbyPlatform"/>
Also if you are using EJB-3.x, no need to make entry of entity beans in persistence.xml, use annotations instead.

Resources