How can I generate entities with IntelliJ without having a session factory?

Viewed 8054

I am using our internal libraries for generating a session factory. This means I don't have an xml file with Hibernate configuration; information like that are read from a configuration file in the container that my application is deployed in. I also don't create a new instance of SessionFactory anywhere in my code; I add a bundle that does that for me.

However, when I try to generate entities with IntelliJ, I get an error saying:

JPA annotation mappings require at least one Session Factory

Considering that I don't have a configuration file, and I don't construct the SessionFactory myself, is there a way to generate annotations?

3 Answers

For Spring Data JPA, add the hibernate.cfg.xml under /resources.

E.g. for postgresql:

<hibernate-configuration>
<session-factory>
    <property name="connection.url">jdbc:postgresql://localhost:5432/db_name</property> 
    <property name="connection.driver_class">org.postgresql.Driver</property> 
    <property name="connection.password"></property> 
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
    <property name="hbm2ddl.auto">update</property> 
    <property name="show_sql">true</property> 
    <property name="format_sql">true</property>
</session-factory>
</hibernate-configuration>

Set your own db url, username, password. Click browse right of "Add to Session Factory" to point to above hibernate.cfg.xml. Then the POJOs will be generated without error "JPA annotation mappings require at least one Session Factory".

After the generation, delete or rename the file hibernate.cfg.xml to hibernate.cfg.xml.bak incase you use it again.

Related