2

I have Hibernate on my project,

but no Spring yet.

I wonder if:

Question 1:

Hibernate annotations and dialect propert in persistence.xml is enough for Hibernate to generate tables?

Question 2: Also i guess, there must be something like Spring to look for annotations and fire a generation event and start some other things?

EDIT:

My persistence.xml file

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Kutuphane2" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>kutuphaneDS</non-jta-data-source>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/kutuphane"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        </properties>
    </persistence-unit>
</persistence>
merveotesi
  • 2,145
  • 15
  • 53
  • 84

1 Answers1

5

Hibernate annotations and dialect propert in persistence.xml is enough for Hibernate to generate tables?

Yes, Hibernate works by itself, Spring just helps Hibernate to provide a data source and help you to access to the Hibernate API.

there must be something like Spring to look for annotations and fire a generation event and start some other things?

Nope, that's Hibernate work as well, just by adding hibernate.hbm2ddl.auto property. This is explained here:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • i have Table and Entity annotations at some classes, and ´ ´ in persistence.xml. There is no error on server start console, but no table generation action. – merveotesi May 15 '14 at 15:02
  • @merveotesi are those classes mapped in your hibernafe.cfg.xml file as well? – Luiggi Mendoza May 15 '14 at 15:03
  • i think your concept of hibernate.cfg.xml is equal to my persistence.xml, and i think those both do not meet the need to generate tables "automatically", they want some boss to tell them what to do, some boss like Spring – merveotesi May 15 '14 at 15:18
  • @merveotesi if you're using JPA, then you should add the configurations on persistence.xml. If you're using Hibernate only, you should post the configurations in hibernate.cfg.xml. Anyway, in the configuration file you should map the classes and add the necessary field for table auto generation. – Luiggi Mendoza May 15 '14 at 15:19
  • Thanks for patience. Do i HAVE TO map them, then why is hibernate annotations for, are they for supporting Spring when it comes, thanks very much – merveotesi May 15 '14 at 15:24
  • 1
    @merveotesi the annotations are to replace XML configuration. But you still need to add in persistence.xml or in hibernate.cfg.xml which are the mapped classes that JPA/Hibernate will use to work with. There's no tool that can auto detect your classes decorated with `@Entity` annotation and say *look, this is a class that will work with JPA*. – Luiggi Mendoza May 15 '14 at 15:27