0

I'm entry level Java developer and I have project with Hibernate and property file hibernate.cfg.xml like this

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC                            
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/account?useSSL=false</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="ua.com.vtkachenko.entity.Group"></mapping>
        <mapping class="ua.com.vtkachenko.entity.Movement"></mapping>
        <mapping class="ua.com.vtkachenko.entity.Product"></mapping>
        <mapping class="ua.com.vtkachenko.entity.Description"></mapping>
    </session-factory>
</hibernate-configuration>

How I can set auto scanning of entity classes and delete this rows?

<mapping class="ua.com.vtkachenko.entity.Group"></mapping>
<mapping class="ua.com.vtkachenko.entity.Movement"></mapping>
<mapping class="ua.com.vtkachenko.entity.Product"></mapping>
<mapping class="ua.com.vtkachenko.entity.Description"></mapping>
Priyank_Vadi
  • 1,028
  • 10
  • 27
Vlad
  • 1
  • 3

3 Answers3

0

When you define sessionfactory bean in spring context file, you can use packagesToScan property and @Entity annotation to achieve this.

Code snippet:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  ... //other properties
  <property name="packagesToScan" value="ua.com.vtkachenko.entity">
  ../other properties
</bean>

Now write @Entity annotation above entity classes. Also your entity class should have @Column annotation to get mapped with table columns.

Hope it helps. If you any issue, please let me know.

SachinSarawgi
  • 2,632
  • 20
  • 28
  • I insert this into my app-context.xml and get error _Exception in thread "main" org.hibernate.MappingException: Unknown entity: ua.com.vtkachenko.entity.Group at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548)..............._ – Vlad Dec 05 '16 at 13:51
  • I think that it is correct way to solve it when you use Spring with Hibernate. In my example I have such project structure ![Screenshot](https://s18.postimg.org/w29vccrih/Screenshot_from_2016_12_05_16_16_19.png). – Vlad Dec 05 '16 at 14:22
0

I would suggest you to learn instead of this: Spring boot and JPA.

It would save you many headaches if your aim is just learning, imho.

Regards

  • Spring Boot and JPA indeed provides a really neat quickstart, but then I got severe headaches while fighting with injections and debugging Spring (Spring is HUGE and overwhelming). I eventually gave up and stopped using Spring and JavaEE altogether, and moved to this: https://github.com/mvysny/vaadin-on-kotlin (disclaimer - I'm the author) – Martin Vysny Jan 25 '17 at 07:54
0

Or, if you don't want to use Spring, here's a solution with pure Hibernate on JavaSE - this will scan your entire classpath for JPA entities: https://stackoverflow.com/a/41845759/377320

Community
  • 1
  • 1
Martin Vysny
  • 3,088
  • 28
  • 39