Setup JPA Console in Intellij using Spring - spring

I'm trying to get the JPA console working in Intellij. I have added JPA to the project under facets. However, IDE is asking for a persistence.xml. I'm using Annotated Spring and don't have any direct mapping files. Anyone configured this before, and can help me? Thanks
Further information:
Without adding JavaEE stuff - I don't need this.
File->Project Structure - Under modules: add JPA to a single module. Do not add persistence.xml or orm.xml
Under facets: Add JPA. Leave XML config blank. Leave JPA Provider blank
View -> Tool windows -> Persistence
You will see the module name and the EntityManager as configured under your SpringContext.xml. On my system I have these properties referenced in this SpringContext.xml
# jdbc.X
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql:///someThing
jdbc.user=postgres
jdbc.pass=postgres
# hibernate.X
hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create
I then select in my Persistence window (in which I can now see all entities) - click console button. Select JPA console. This will get me the error outlined here {yes, I'm using Postgres}
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206186929-JPA-Console-without-persistence-xml
Note the two "TTs-" PostgreSQL9Dialectt
Further further information:
Wierd. If I move the dialect into the actual xml config
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<!--<prop key="hibernate.enable_lazy_load_no_trans">true</prop>-->
</props>
</property>
[2017-01-04 13:03:46] java.lang.IllegalStateException: Transaction already active
at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)
at com.intellij.jpa.remote.impl.RemoteQueryImpl$1.getResultList(RemoteQueryImpl.java:92)
at com.intellij.jpa.remote.impl.QueryResultImpl.ensureInitialized(QueryResultImpl.java:113)
at com.intellij.jpa.remote.impl.QueryResultImpl.getColumnInfos(QueryResultImpl.java:30)
at com.intellij.jpa.remote.impl.RemoteQueryResultImpl.getColumnInfos(RemoteQueryResultImpl.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Related

Turning off net.sf.ehcache and org.hibernate DEBUG logging

I am getting a host of DEBUG messages from net.sf.ehcache and org.hibernate in my Spring (version 5.2.5.RELEASE) application. I am using version 5.2.10.Final of hibernate-ehcache. The messages look like this:
16:20:12.910 [RMI TCP Connection(3)-127.0.0.1] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_COMMIT
16:20:12.910 [RMI TCP Connection(3)-127.0.0.1] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_ROLLBACK
16:20:12.910 [RMI TCP Connection(3)-127.0.0.1] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_RECOVERY
and also many like this:
1:14:39.284 [http-nio-8080-exec-3] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Done materializing entity [com.acme.MyBean#145]
I've tried to turn them down by using this in log4j.xml
<logger name="net.sf.ehcache">
<level value="WARN"/>
</logger>
I've also tried this in application.properties:
logging.level.org.springframework.web=WARNING
logging.level.org.hibernate=ERROR
logging.level.org.springframework.cache=WARNING
But neither of these changes filters out the DEBUG messages.
Here is the ehcache configuration as defined in spring-config.xml:
<bean id="readOnlyDatabaseSessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">${hibernate.debug}</prop>
<prop key="hibernate.format_sql">${hibernate.debug}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.debug}</prop>
<prop key="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
</prop>
<prop key="net.sf.ehcache.configurationResourceName">
ehcache.xml
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.default_batch_fetch_size">100</prop>
</props>
</property>
<property name="dataSource" ref="readOnlyDataSource"/>
</bean>
I have the following settings in application.properties:
# logging for hibernate
logging.level.org.hibernate.SQL=warn
logging.level.org.hibernate.stat=warn
logging.level.org.hibernate.type=warn
logging.level.org.springframework.web=WARN
logging.level.org.hibernate=ERROR
logging.level.org.springframework.cache=WARN
logging.level.root=WARN
logging.level.net.sf.ehcache=WARN
spring.jpa.properties.hibernate.generate_statistics=false
How do I turn off DEBUG messages for net.sf.ehcache and org.hibernate?
If you want to turn off the logging on a specific package you should use the value off. This will work both on package and class level like:
logging.level.org.hibernate.SQL=OFF
com.yourproject.util.SomeUtilsClass=OFF
Normally logging.level.net.sf.ehcache=WARN should work, but can you try:
logging.level.net.sf.ehcache.statistics.extended=WARN
If you're running a junit test you should update/create different config for that because by default junit will use DEBUG as the root log level where when you launch your springboot app it will use by default INFO and what you customized in your application.properties.
Welcome in the world of Open Source where you have many options to choose from, but documentation is scarce.
I don't know whether you use only Spring or also Spring Boot, how you start your application, and without having a reproducable scenario it is impossible to give a precise answer so here comes a bit longer and more complex answer:
application.properties might work if you use the right logging libraries (e.g. in pom.xml ) and the right way to initialize spring boot (which you probably don't use)
if you are sure that you use spring boot (dependencies + initialization) but your configuration files are ignored although the application is reading them it may be that you override the settings using e.g.: environment variables, see the 17 different ways to externalize configuration for spring boot 1.2
if you use maven you may want to get a clearer picture of which logging frameworks you use by executing some of these commands: mvn dependency:tree or mvn help:effective-pom or in any case look at the Java classpath for which files and directories your logging libraries and configuration files might be loaded from and read their respective documentation on how to configure them (which is very different e.g. for the non-backwards compatible log4j2 than for log4j 1.x, logback, etc.)
before you start pulling your hair in desperation because you really cannot find where your application tries to load logging configuration from, run a tracing tool for your operating system to see which files it opens and reads during startup, e.g.: strace (Linux), sysinternals process monitor (Windows) offer insight into the interaction between your application (Java) and the operating system
Finally, be aware that some logging frameworks (e.g.: log4j2, slf4j) have been split in multiple parts, e.g.:
for the programmer to program/compile against
for the runtime to write the logging to a file, over the network or to another logging framework
integration to also receive logging from other framworks
See for some great examples how you can use that:
for Log4j2 this documentation
for SLF4J/logback that documentation

generate statistics hibernate not showing insert/update stats

I am using Spring+JPA+Hibernate
This is how my bean is defined
<bean id="entityManager"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaProperties">
<props>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
<.....other properties.......... >
</bean>
The generate_statics is only showing statistics for select queries.
How can we see statistics for inserts?
Is there a simple configuration that can be used to show all the statistics where we can see hibernate entity to query translation time and insert/update time to db?
I enabled the statistics using the following steps
Include the following dependency in your pom
org.lazyluke
log4jdbc-remix
0.2.7
replace your driver class with net.sf.log4jdbc.DriverSpy
enable the required in your log4j properties
log4j.logger.jdbc.sqlonly=OFF
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=ERROR
log4j.logger.jdbc.connection=ERROR
log4j.logger.jdbc.resultsettable=OFF
4.Add log4jdbc to the connection url
for example: i am using oracle db. so i changed the connection url from
jdbc:oracle:thin:#(hostname:port)
jdbc:log4jdbc:oracle:thin:#(hostname:port)

Not Creating Tables in Hibernate JPA

I am creating Spring (Version 4.0.6) , Hibernate(Version 4.3.6) CRUD application.
I tried giving both <prop key="hibernate.hbm2ddl.auto">create</prop> and <property name="generateDdl" value="true" />. But the tables did not get created.
All entities has #Entity and #Table(name) annotation from javax.persistence.
Here comes my applicationContext.xml. http://textuploader.com/5pjw8
Here comes one of my Entity
http://textuploader.com/5pjyk
Where I have gone wrong and why it is not creating tables for me?
How do I troubleshoot the issue?
You don't seem to have defined a datasource bean, i.e. an actual database to connect to, is this in another file?

Hibernate doesn't seem to be scanning for #Entity #Table

I've got two Spring Hibernate modules that I'm trying to combine. They have separate application contexts. In one hibernate uses xml mapping files while in the second module annotations are used to map the hibernate db tables.
Both modules seem to be scanned ok (service beans seem to be injected ok) except for the hibernate table scanning in the second table. One table occurs twice -- once in the xml mapping files and once in annotations but in different java packages with separated package scanning.
When I try to access the second module's table class in a hibernate select I get:
MappingException: Unknown entity: msg.entity: message
I tried substituting another annotated table entity in the hibernate query to check where the problem was mapping to the same table but I get the same MappingException so it seems like the problem is that hibernate is just not picking up any #Entity #Table annotations in the second module. But I tested this module on it's own and all the annotations did get picked up ok.
Could it be that the hibernate session factory is somehow being carried over from the calling module and masking the second module's session factory and its associated list of entities? Yet I'm not in a transaction in the calling method.
<context:component-scan base-package="msg"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceJmsMod"/>
<property name="packagesToScan" value="msg.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
Thanks for any help

How to integrate hibernate with spring?

I am newbie in spring. using spring 3.0 mvc. I am creating a spring application, I have a login form,Any one please suggest how to integrate hibernate and its set up...
You can define Hibernate's Session Factory bean in Spring's application context of your application. Look at this example:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>dao/hibernate/Login.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
where:
dataSource - reference to some implementation of javax.sql.DataSource
mappingResources - list of Hibernate ORM mapping files (shoul be on the classpath)
hibernateProperties - some basic set of Hibernate properties, you should tell Hiberante at least what type of DB you are using
Then you can wire this bean into DAO classes of your application and perform CRUD operations using wired sessionFactory object.
Suggestion for the future: you should be more specific in your questions!
You can use Springfuse. It's a tool which build a maven projet with Spring, Spring-mvc and Hibernate. It can provides you some demos (very usefull for newbies).
Not really a tutorial but, for an overview of the Spring 3.0 web stack, I'd recommend... Overview of the Spring 3.0 Web Stack.
In this presentation from SpringOne 2009, Keith Donald discusses the Spring 3.0 web stack, key Spring Framework and Spring MVC features, demos of Spring MVC capabilities, REST support, validation support, automatic data conversion, data binding and validation, Joda Time support, Spring JavaScript, Dojo, Spring Web Flow, Spring Security, Spring BlazeDS, and the roadmap for the Spring web stack.
For the persistence, maybe have a look at JPA 2.0 and Spring 3.0 with Maven. But the documentation is still the best resource IMO. See the 13. "Object Relational Mapping (ORM) Data Access"
Regarding the IDE, Spring Tool Suite will indeed give you the best user experience
For example, refer below links:
http://www.mkyong.com/struts/struts-spring-hibernate-integration-example/
http://www.vaannila.com/spring/spring-hibernate-integration-1.html

Resources