Lately I found an example of Spring Boot CRUD. In the read me there is written :
This project is based on the Spring Boot project and uses these
packages :
Maven
Spring Core
Spring Data (Hibernate & MySQL)
Spring MVC (Tomcat)
Thymleaf
In the source code I do not see anything that would look like this app is somehow connected to the hibernate. Could you help me to solve this little problem? And if it is not connected to the Hibernate how can I connect CRUD like that to the Hibernate?
Thanks for your help :)
In example you've provided you're using spring-boot-starter-data-jpa which already contains predefined hibernate dependencies (see pom.xml).
How to work with SQL databases described in documentation section.
Basically you configure hibernate using application.properties using following prefix:
spring.jpa.properties.hibernate.*
for Spring boot with hibernate you can follow bellow link :-
https://github.com/netgloo/spring-boot-samples
you have to configure hibernate property and datasource property for database connection... but for example i can share some code for Spring hibernate and JPA but Spring boot with hibernate you can follow link:-
<bean id="hibernateJpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.amstech.mayal.entity" />
<property name="jpaDialect" ref="hibernateJpaDialect" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.connection.driver_class" value="${database.jdbc.driver.class}" />
<entry key="hibernate.connection.url" value="${database.jdbc.url}" />
<entry key="hibernate.connection.username" value="${database.user}" />
<entry key="hibernate.connection.password" value="${database.password}" />
<entry key="hibernate.dialect" value="${hibernate.dialect}" />
<entry key="show_sql" value="true" />
<entry key="eclipselink.jdbc.exclusive-connection.is-lazy"
value="true" />
</map>
</property>
</bean>
I would suggest looking at the Spring Boot Data section of the main documentation. There is a lot less configuration that is needed and you can do it fluently and leave the xml behind. JPA + Hibernate is Spring data have become highly interlinked in boot.
There are multiple ways in which spring boot interact with hibernate. In the example you shared it is picking up the db properties from application.properties file and setting up the configuration. Rest of the things it will pick from the dependency provide in pom.xml.
Yes, it is connected with the hibernate. Things you need to do apart from setting up the project is to setting up a database with some username and password. And creating a db schema.Rest of the things will be done by spring boot. Make sure your db username password matches with the application file properties.
Related
As I am planning to update from "hibernate3" to "hibernate4" & "spring 3.0.5" to "spring 3.1.4".
I have configured OpenSessionInViewInterceptor in spring 3.0.5 so want to configure same in 3.1.4.
But I am not able to configure flushMode in OpenSessionInViewInterceptor of Spring 3.1.4;
My Previous setting for spring 3.0.5 was:
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushMode">
<bean
id="org.springframework.orm.hibernate3.HibernateAccessor.FLUSH_NEVER"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
Now tried to configure same for spring 3.1.4 as below:
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushMode">
<bean
id="org.springframework.orm.hibernate3.HibernateAccessor.FLUSH_NEVER"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
then it throws below exception:
org.springframework.beans.NotWritablePropertyException: Invalid property 'flushMode' of bean class [org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor]: Bean property 'flushMode' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
And there is no similar class found in alternate to org.springframework.orm.hibernate3.HibernateAccessor in spring 3.1.4
So my question is how to set flushMode property of OpenSessionInViewInterceptor of spring 3.1.4 ?
It looks like a mess, with unbound links to property accessors. I'd guess that a copy-paste job was done without much thinking about cleaning things up given the different inheritance hierarchies. I hate it when that happens…
Can you use the Hibernate 3 version instead? Yes, it really does appear to be there; here's the link: org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
Longer term, look more carefully whether the Hibernate 4 code does what you want without specifying the flag at all. Unfortunately, you'll have to ignore the documentation (at least for now) and study the source itself.
I'm used to have persistence.xml in my projects in which I define the provider to use (hibernate in most cases).
However, I'm starting a new project in which it is mandatory to use Spring framework. I've seen some blogs describing the integration of Hibernate in Spring and I've understood that I should declare a session factory in spring's beans descriptor org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
All the examples I've seen don't mention the use of persistence.xml, persistence context, entity manager...
I'm not sure I understand this point, I always thought that Hibernate is just a provider of JPA unless the factory declared in application-context.xml is doing something in background. If it is the case, I would like to understand how it is working..
thanks in advance...
AnnotationSessionFactoryBean is Factory bean implemented by Spring to create Hibernate Session Factory and shared to Spring's Application Context.if you are planning to use Direct Hibernate ( in case you dont need persistent.xml / per-cont.xml / entityManager) you can provide the properties in AnnotationSession FactoryBean. and can be injected in Any DAO.
How ever if you are planing to wire through JPA. Then you need ( persistent and persistent-context and entity Manager). In order to do that you required three steps
1. declare / configure Spring's JPA Adapter to create EntityManager instance for you
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
Inside JPA Bean declaration provide details about your database and who is ORM provider such as ( hibernate /toplink / ....) in your case Hibernate
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle9Dialect"/>
</bean>
</property>
then Provide information about your persistent entity details in persistent.xml or some way
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
if you have any specific JPA properties then
<property name="jpaProperties">
<props>
<!-- <prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
-->
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
Bottom line, Spring provies adapters to directly to Hibernate ( in that case your dont need Jpa files such as persistent.xml and so but you need hibernate related files like hbm files) or adapters to the JPA ( in that case you have provide details about who is JPA vendor and instrut spring how to connect to the JPA vendor).
Hope the above clarifies.
I have a Web service application based on JPA (Hibernate), Spring and CXF.
I am facing some lazy-load exceptions after transactional business methods (because I need some extra beans to be rendered in the rpesentation layer), and I wanted to give a try to the OpenSession/EntityManagerInView pattern.
Please do not argue this choice, we are just giving it a try.
The issue is that, since I am using CXFServlet, instead of standard Spring Servlet, I cannot use OpenEntityManagerInViewFilter in web.xml.
I cannot use either OpenEntityManagerInViewInterceptor which applies as a WebRequest Interceptor (and does not work with CXF interceptor/filters).
Finally I am aware of the HibernateInterceptor, an AOP proxy which wraps any method into a session. But still : this one is for the Hibernate API, not JPA API (I am using EntityManagerFactory, rather than SessionFactory).
So, are you aware of either :
A HibernateInterceptor for the JPA API (EntityManagerInterceptor ?)
A way to adapt a Spring WebRequestInterceptor into a JAX-RSfilter ?
Any other solution ?
Thanks in advance for your help.
Once again, I finally found what I wanted ...
There is actually a JpaInterceptor that does what I want (it seems deprecated though. I do not really understand why).
Here is the resulting configuration that works like a charm, with a bit of auto proxy by name :
<bean id="jpaInterceptor" class="org.springframework.orm.jpa.JpaInterceptor">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jpaAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<property name="beanNames">
<list>
<value>myService1</value>
<value>myService2</value>
<value>...</value>
</list>
</property>
<property name="interceptorNames">
<list><value>jpaInterceptor</value></list>
</property>
</bean>
<jaxrs:server id="services" address="/">
<jaxrs:serviceBeans>
<ref bean="myService1" />
<ref bean="myService2" />
<ref bean="..." />
</jaxrs:serviceBeans>
</jaxrs:server>
Spring web flow provides additional bean scopes like flow, conversation, flash etc. I can define flow scope beans in flow.xml using var or i can set values to new scoped variables. How i can define it in spring application context xml file. I tried to use this pattern:
<bean id="abc" class="abc" scope="flow"/>
I got error that no scope defined. I searched on google and found this thing
http://blog.springsource.org/2007/05/08/spring-web-flow-bean-scopes-and-jsf/
but don't know how to enable it in spring web flow 2.3
try to define it in your application context:
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="flow">
<bean class="org.springframework.webflow.config.scope.FlowScope"/>
</entry>
</map>
</property>
</bean>
Previously, my Java web projects used Eclipse-ordinary structure, and at the start of the container (in case, Tomcat), Hibernate generated the schemes correctly.
Now I'm using Maven infrastructure. I've relocated the needed files and configured all well (I think, because all is working right: Spring is starting, Hibernate is connecting the database - when it was previously created and there's some data to fetch). I've tested all CRUD operations and it's working.
The problem is that Hibernate refuses to generate the schemes (DDL) as it did when over Eclipse-ordinary infrastructure.
Additional information:
My persistence.xml is almost empty (as always) because Spring applicationContext.xml is starting it. I have not changed the file, it continues the same way as before.
<!-- Location: src/main/resources/META-INF/persistence.xml -->
<persistence>
<persistence-unit name="jpa-persistence-unit" transaction-type="RESOURCE_LOCAL"/>
</persistence>
Part of the Spring configuration goes here (applicationContext.xml):
<!-- Location: src/main/webapp/WEB-INF/applicationContext.xml -->
<!-- ... -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="[DATABASE-NAME]" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" /> <!-- THIS CONFIGURATION WORKED PREVIOUSLY, NOW WITH MAVEN, IT'S IGNORED -->
<property name="databasePlatform" value="[DIALECT]" />
</bean>
<!-- ... -->
I'm not using any Maven Hibernate plugin, because I just want the default behavior that occurred earlier.
Did Maven invalidate this "generateDdl" property!? Why!? What should I do!? I can't find any solution.
I found out the solution.
Maven has any fault about that.
Hibernate was not able to create my database because the "DIALECT" was wrong.
I remembered that I changed the dialect from MySQL to MySQL-InnoDB. Hibernate was logging this problem but I couldn't see it because the slf4j-simple dependency was not explicity imported.
Thank you for your time, Shawn.