Dodawanie rekordów do bazy - błędy.

0

Stram się utworzyć połączenie z klasa i dodanie nowych rekordów ale mam błędy ale nie dla czego. Utworzyłem samą bazę bez tabel i używam mavena. Starałem się sam poradzić sobie z tym ale bez skutecznie.

persistance.xml

<persistence 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"
             version="2.0">
    <persistence-unit name="ShoppingDB" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.speed.model.Category</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:mysql://localhost:3306/shopping"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="xxx"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

Klasa

@Entity
@Table(name="Category")
public class Category implements Serializable{


    @Id
    @Column(name = "catId")
    @NotNull
    private int catId;
    @Column(name = "catName")
    private String catName;
    @Column(name = "catParent")
    private int catParent;
    @Column(name = "catPosition")
    private int catPosition;
    @Column(name = "catIsProductCatalogueEnabled")
    private int catIsProductCatalogueEnabled;

    public Category(int catId, int catParent, String catName) {
        this.catId = catId;
        this.catName = catName;
        this.catParent = catParent;
    }

    public Category() {

    }

    public int getCatId() {
        return catId;
    }

    public void setCatId(int catId) {
        this.catId = catId;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public int getCatParent() {
        return catParent;
    }

    public void setCatParent(int catParent) {
        this.catParent = catParent;
    }

    public int getCatPosition() {
        return catPosition;
    }

    public void setCatPosition(int catPosition) {
        this.catPosition = catPosition;
    }

    public int getCatIsProductCatalogueEnabled() {
        return catIsProductCatalogueEnabled;
    }

    public void setCatIsProductCatalogueEnabled(int catIsProductCatalogueEnabled) {
        this.catIsProductCatalogueEnabled = catIsProductCatalogueEnabled;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Category category = (Category) o;

        if (getCatId() != category.getCatId()) return false;
        if (getCatParent() != category.getCatParent()) return false;
        if (getCatPosition() != category.getCatPosition()) return false;
        if (getCatIsProductCatalogueEnabled() != category.getCatIsProductCatalogueEnabled()) return false;
        return getCatName().equals(category.getCatName());

    }

    @Override
    public int hashCode() {
        int result = getCatId();
        result = 31 * result + getCatName().hashCode();
        result = 31 * result + getCatParent();
        result = 31 * result + getCatPosition();
        result = 31 * result + getCatIsProductCatalogueEnabled();
        return result;
    }

    @Override
    public String toString() {
        return "Category{" +
                "catId=" + catId +
                ", catName='" + catName + '\'' +
                ", catParent=" + catParent +
                ", catPosition=" + catPosition +
                ", catIsProductCatalogueEnabled=" + catIsProductCatalogueEnabled +
                '}';
    }
}

Testowe Dao

public class Test {


    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ShoppingDB");
        EntityManager em = emf.createEntityManager();

        Category category = new Category();
        category.setCatId(1);
        category.setCatName("Test");
        category.setCatParent(11);
        category.setCatPosition(111);

        em.getTransaction().begin();
        em.persist(category);
        em.getTransaction().commit();

        category = em.find(Category.class,1);
        System.out.println(category);


    }
}

I błedy

2016-05-12 08:51:18 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2016-05-12 08:51:18 DEBUG SQL:111 - 
    insert 
    into
        Category
        (catIsProductCatalogueEnabled, catName, catParent, catPosition, catId) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        Category
        (catIsProductCatalogueEnabled, catName, catParent, catPosition, catId) 
    values
        (?, ?, ?, ?, ?)
2016-05-12 08:51:18 DEBUG AbstractBatcher:66 - Executing batch size: 1
2016-05-12 08:51:18 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2016-05-12 08:51:18 DEBUG JDBCExceptionReporter:225 - Could not execute JDBC batch update [insert into Category (catIsProductCatalogueEnabled, catName, catParent, catPosition, catId) values (?, ?, ?, ?, ?)]
java.sql.BatchUpdateException: Table 'shopping.category' doesn't exist
	at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1819)
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1281)
	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)
	at com.speed.Test.main(Test.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'shopping.category' doesn't exist
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
	at com.mysql.jdbc.Util.getInstance(Util.java:372)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1911)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2145)
	at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1777)
	... 17 more
2016-05-12 08:51:18 WARN  JDBCExceptionReporter:233 - SQL Error: 1146, SQLState: 42S02
2016-05-12 08:51:18 ERROR JDBCExceptionReporter:234 - Table 'shopping.category' doesn't exist
2016-05-12 08:51:18 DEBUG AbstractEntityManagerImpl:1116 - mark transaction for rollback
2016-05-12 08:51:18 DEBUG JDBCTransaction:182 - rollback
2016-05-12 08:51:18 DEBUG JDBCTransaction:223 - re-enabling autocommit
2016-05-12 08:51:18 DEBUG JDBCTransaction:193 - rolled back JDBC Connection
2016-05-12 08:51:18 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection
2016-05-12 08:51:18 DEBUG ConnectionManager:464 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
	at com.speed.Test.main(Test.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
	at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
	at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81)
	... 6 more
Caused by: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)
	... 6 more
Caused by: java.sql.BatchUpdateException: Table 'shopping.category' doesn't exist
	at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1819)
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1281)
	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
	... 14 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'shopping.category' doesn't exist
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
	at com.mysql.jdbc.Util.getInstance(Util.java:372)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1911)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2145)
	at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1777)
	.
0

Moze sprobuj zmienic zapis tej linii:

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

na:

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

NIestety nic to nie dalo

1 użytkowników online, w tym zalogowanych: 0, gości: 1