5

I am using eclipse IDE.I also try following two .but failed..when i manually create table in my mysql database then my complete program run fine... I want create table automatic with respect to entity class.

<property name="hibernate.hbm2ddl.auto" value="create"/>

<property name="hibernate.hbm2ddl.auto">update</property>

here my persistence 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="JpaTest2" transaction-type="RESOURCE_LOCAL">

     <class>com.jpa.Employee</class>

    <properties>

        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate"/>
        <property name="javax.persistence.jdbc.user" value="umar"/>
        <property name="javax.persistence.jdbc.password" value="umar"/>
        <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"/>


    </properties>
</persistence-unit>

3
  • Do you think there's additional information that could help people trying to understand what's going on here? Or is this it? Commented Sep 30, 2015 at 19:27
  • markdown code properly Commented Oct 3, 2015 at 8:24
  • What's the expection after adding <property name="hibernate.hbm2ddl.auto" value="create"/> in persistence.xml? Commented Oct 3, 2015 at 8:31

2 Answers 2

16

Dont use Hibernate specific options. JPA 2.1 provides

javax.persistence.schema-generation.database.action

that can be set to "create", "drop", "drop-and-create", "none". That way you keep your code JPA implementation independent

Sign up to request clarification or add additional context in comments.

4 Comments

Seems to work for me. I agree that this is a preferred solution in order to make your bundles more generic
How do you prevent it from trying to create the same table on each restart?
@KennySteegmans If it already created the database, nothing will happen on next restart when set to 'create'
yes,perfect..@Neil Stockton
0

Check your entity. Did you miss @Table annotation? The exception clearly says that the table is missing 'hibernate.employee':

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate.employee' doesn't exist at ...

If you defined a naming strategy that prepends all tables with hibernate., then make sure that the tables are created in MySql.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.