-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate EntityManager and MySQL : Cannot persist data.
PostPosted: Wed Oct 24, 2007 7:05 am 
Newbie

Joined: Wed Oct 24, 2007 6:25 am
Posts: 4
Hi,

I'm starting a new project from scratch, and doing it small for introducing myself to the Hibernate EntityManager, Annotations, JPA, ...

After struggling a bit to make the project work, I found some errors that I cannot find answers for.

I'm using maven for builds, and eclipse 3.2.2 for development. I included (at least I believe so) all dependencies needed, as they are used in the maven repository.

Database is a MySQL 5.1.22, with MySQL Connector 5.1.5.

I have created a table TBMPTYPE, and configured a Java Class for proper mapping. The Id is table generated ( @GeneratedValue(strategy=GenerationType.TABLE) ) and all fields seem to be correctly mapped.

When I first run the application, there was a crash. Something like "hibernate_sequences" table not being found. I found it strange as I configured and created a table for the table id generated effect:

@Id @GeneratedValue(strategy=GenerationType.TABLE) @TableGenerator(name="TBMPTYPE", table="TBMPKGEN", pkColumnName="KGEN_KTBL", valueColumnName="KGEN_KVAL")
@Column(name="PK_TYPE") private Long typeId;

I included the following entry on the persistence.xml:

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

And running again, I found it was trying to create the hibernate_sequences table and also the TBMPTYPE table. The user only has select/insert/update/delete permissions, so it could not create them.

Then I logged in manually using the root user, and created the table hibernate_sequences with the definition specified by hibernate in the log info.

After running again, it generates a new id, using the hibernate_sequences table, but still it cannot find the TBMPTYPE table I created before.

I don't know what's happening, since I defined a specific table for id generator, shouldn't it use it? What or why does it use a hibernate_sequences when I don't define it?

I have no class mapped to the table generator, as I think it's not required by the jpa spec.


Any help appreciated. Thanks.


Some more info:


Code:
-- Tables ----------

create table TBMPKGEN
(
   KGEN_KTBL            varchar(100) not null,
   KGEN_KVAL            bigint not null,
   primary key (KGEN_KTBL)
);

insert into tbmpkgen (kgen_ktbl, kgen_kval) values ('TBMPTYPE', 0);


create table TBMPTYPE
(
   PK_TYPE              bigint not null,
   TYPE_DESC            char(200) not null,
   primary key (PK_TYPE)
);


select * from mp.hibernate_sequences;

/*
sequence_name   sequence_next_hi_value
TBMPTYPE   3
*/


-- Java Class Mapped ----------

package com.mp.persistence.entity;

import javax.persistence.*;

@Entity
@Table(name="TBMPTYPE", schema="mp")
public class Type {

   @Id
   @GeneratedValue(strategy=GenerationType.TABLE) @TableGenerator(name="TBMPTYPE", table="TBMPKGEN", pkColumnName="KGEN_KTBL", valueColumnName="KGEN_KVAL")
   @Column(name="PK_TYPE") private Long typeId;
   @Column(name="TYPE_DESC") private String description;

   public Type() {
      super();
   }

   public String getDescription() {
      return description;
   }

   public void setDescription(String description) {
      this.description = description;
   }

   public Long getTypeId() {
      return typeId;
   }

   public void setTypeId(Long typeId) {
      this.typeId = typeId;
   }

}


-- persistence.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_1_0.xsd" version="1.0">

   <persistence-unit name="MPService" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>com.mp.persistence.entity.Type</class>
      <properties>
         <property name="hibernate.archive.autodetection" value="class"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.format_sql" value="true"/>
         <property name="hibernate.use_sql_comments" value="true" />

         <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mp"/>
         <property name="hibernate.connection.username" value="myuser"/>
         <property name="hibernate.connection.password" value="mypass"/>

         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

         <property name="hibernate.hbm2ddl.auto" value="create"/>
      </properties>
   </persistence-unit>

</persistence>


-- Execution Code ----------

      System.out.println(">>> execute");

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

      Type type = new Type();
      type.setDescription("First Description");

      em.persist(type);

      System.out.println("<<< execute");


--

>>> execute
24/Out/2007 12:05:06 org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.0.GA
24/Out/2007 12:05:06 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.4
24/Out/2007 12:05:06 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
24/Out/2007 12:05:06 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
24/Out/2007 12:05:06 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
24/Out/2007 12:05:06 org.hibernate.ejb.Version <clinit>
INFO: Hibernate EntityManager 3.3.1.GA
24/Out/2007 12:05:07 org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: com.mp.persistence.entity.Type
24/Out/2007 12:05:07 org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity com.mp.persistence.entity.Type on table TBMPTYPE
24/Out/2007 12:05:07 org.hibernate.validator.Version <clinit>
INFO: Hibernate Validator 3.0.0.GA
24/Out/2007 12:05:07 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
24/Out/2007 12:05:07 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
24/Out/2007 12:05:07 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: true
24/Out/2007 12:05:07 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/mp
24/Out/2007 12:05:07 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=myuser, password=****, autocommit=true, release_mode=auto}
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.1.22-rc-community-log
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.5 ( Revision: ${svn.Revision} )
24/Out/2007 12:05:07 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
24/Out/2007 12:05:07 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
24/Out/2007 12:05:07 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: enabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
24/Out/2007 12:05:07 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: enabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
24/Out/2007 12:05:07 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
24/Out/2007 12:05:07 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
24/Out/2007 12:05:08 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
24/Out/2007 12:05:08 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: Running hbm2ddl schema export
24/Out/2007 12:05:08 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: exporting generated schema to database
24/Out/2007 12:05:08 org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: Unsuccessful: create table mp.TBMPTYPE (PK_TYPE bigint not null, TYPE_DESC varchar(255), primary key (PK_TYPE))
24/Out/2007 12:05:08 org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: CREATE command denied to user 'myuser'@'localhost' for table 'tbmptype'
24/Out/2007 12:05:08 org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: Unsuccessful: create table hibernate_sequences ( sequence_name varchar(255),  sequence_next_hi_value integer )
24/Out/2007 12:05:08 org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: CREATE command denied to user 'myuser'@'localhost' for table 'hibernate_sequences'
24/Out/2007 12:05:08 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: schema export complete
<<< execute


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 2:07 pm 
Newbie

Joined: Mon Oct 01, 2007 12:42 pm
Posts: 13
Location: Helsinki, Finland
You are not using the table generator you have defined. You need to add the generator name:

Code:
@GeneratedValue(strategy=GenerationType.TABLE,generator="TBMPTYPE")


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 12:22 pm 
Newbie

Joined: Wed Oct 24, 2007 6:25 am
Posts: 4
Immo Heikkinen wrote:
You are not using the table generator you have defined. You need to add the generator name:

Code:
@GeneratedValue(strategy=GenerationType.TABLE,generator="TBMPTYPE")


Thanks for the answer, I was lead to error by a manual that didn't state that in the example.

The example they gave was:

@TableGenerator(name="Emp_Gen")
@Id @GeneratedValue(generator="Emp_Gen")
private int id;


Now I'm having another trouble ... it does update the generator table correctly, but it still does not persist data on the TBMPTYPE ...

The code:

System.out.println(">>> execute");

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

Type type = new Type();
type.setDescription("First Description");

em.persist(type);

System.out.println("<<< execute");


Should be able to persist, still it doesn't do anything.

Any idea I should search for?

My logs:

...
16:15:18,968 DEBUG org.hibernate.impl.SessionFactoryObjectFactory:39 - initializing class SessionFactoryObjectFactory
16:15:18,968 DEBUG org.hibernate.impl.SessionFactoryObjectFactory:76 - registered: 2c9285c215ec8bb00115ec8bb1b80000 (unnamed)
16:15:18,968 INFO org.hibernate.impl.SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
16:15:18,968 DEBUG org.hibernate.impl.SessionFactoryImpl:308 - instantiated session factory
16:15:18,968 DEBUG org.hibernate.impl.SessionFactoryImpl:392 - Checking 0 named HQL queries
16:15:18,968 DEBUG org.hibernate.impl.SessionFactoryImpl:412 - Checking 0 named SQL queries
16:15:19,031 DEBUG org.hibernate.impl.SessionImpl:220 - opened session at timestamp: 11936745189
16:15:19,031 DEBUG org.hibernate.event.def.AbstractSaveEventListener:514 - transient instance of: com.mp.persistence.entity.Type
16:15:19,031 DEBUG org.hibernate.event.def.DefaultPersistEventListener:124 - saving transient instance
16:15:19,031 DEBUG org.hibernate.jdbc.AbstractBatcher:556 - opening JDBC connection
16:15:19,031 DEBUG org.hibernate.connection.DriverManagerConnectionProvider:93 - total checked-out connections: 0
16:15:19,031 DEBUG org.hibernate.connection.DriverManagerConnectionProvider:99 - using pooled JDBC connection, pool size: 0
16:15:19,031 DEBUG org.hibernate.SQL:132 - select KGEN_KVAL from TBMPKGEN where KGEN_KTBL = 'TBMPTYPE' for update
16:15:19,046 DEBUG org.hibernate.jdbc.AbstractBatcher:571 - closing JDBC connection (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)
16:15:19,062 DEBUG org.hibernate.connection.DriverManagerConnectionProvider:129 - returning connection to pool, pool size: 1
16:15:19,062 DEBUG org.hibernate.id.MultipleHiLoPerTableGenerator:194 - new hi value: 9
16:15:19,062 DEBUG org.hibernate.event.def.AbstractSaveEventListener:112 - generated identifier: 450, using strategy: org.hibernate.id.MultipleHiLoPerTableGenerator
16:15:19,062 DEBUG org.hibernate.event.def.AbstractSaveEventListener:153 - saving [com.mp.persistence.entity.Type#450]


Any help appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 1:40 pm 
Newbie

Joined: Wed Oct 24, 2007 6:25 am
Posts: 4
Found the problem! Forgot to start a transaction! eh :)

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.