2

Here is some entity:

@Entity public class Forest { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; public Forest() { } public long getId() { return id; } public void setId(long id) { this.id = id; } } 

I want to insert some element in table forest:

public class Main { private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("server"); public static void main(String[] args) { EntityManager em = emf.createEntityManager(); EntityTransaction trx = em.getTransaction(); Forest forest = new Forest(); trx.begin(); em.persist(forest); trx.commit(); } } 

Thrown exception:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Table 'server.forest' doesn't exist Caused by: org.hibernate.exception.SQLGrammarException: Table 'server.forest' doesn't exist 

My persistence.xml file with settings:

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="server"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/server"/> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="root" /> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence> 

When I removed @GeneratedValue(strategy = GenerationType.AUTO) and set id for forest: forest.setID(1), there was no exception and table has been generated. So, auto-generating of id is not working and I don't know why.

1
  • which version of hibernate? Commented Feb 15, 2013 at 19:44

1 Answer 1

4

According configuration there is org.hibernate.dialect.HSQLDialect used with MySQL database. Using MySQL dialect instead of one of HSQL likely helps. Likely InnoDB is used - if so, then MySQL5InnoDBDialect is way to go.

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

1 Comment

Thank you very much! I'm so innatentive!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.