Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Is Hibernate worth it?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Location
    Denmark
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Is Hibernate worth it?

    Hey I am writing an JSP based web application for maintaing and keep inventory of 2000+ books.
    The data will be store persistent in a MySQL or PostgreSQL DB (have not decide yet), and I am use to doing things by JPA and String based SQL statements.
    But a friend of mind told me about Hibernate and I find the idea of an ORM thingy intriguing, but is it worth the time to set up?
    The classes I will be working with is:
    User
    Book
    BookExtra
    Author
    Publisher

    more about the model
    A Book contains title, ISBN and so one and foreign keys to BookExtra, Author and Publisher, a user can rent a book to another user.
    Ruby 'cause I can - Java 'cause I want - C# 'cause I have to


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Is Hibernate worth it?

    Worth it? Absoloutely. Necessary? not at all. In my personal experience, maintaining JDBC can be a bitch compared to hibernate so this alone could be enough to win someone over. In the end however, there is no simple answer as each have their own benefits and tradeoffs. For a relatively simple application as you're suggesting though, I'd say its worth using Hibernate. Why? 1. It is a great project to learn with, and 2. It will increase your productivity as you can quickly get into logic instead of dealing with the hassle of result sets etc.

    What ever you do however, best of luck.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Location
    Denmark
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Is Hibernate worth it?

    Okay now I am going with Hibernate and get this error:
    Hibernate: drop table if exists Book
    Hibernate: create table Book (id integer not null auto_increment, createdDate datetime, isbn varchar(255), title varchar(255), primary key (id))
    okt 07, 2013 5:19:56 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
    ERROR: HHH000389: Unsuccessful: create table Book (id integer not null auto_increment, createdDate datetime, isbn varchar(255), title varchar(255), primary key (id))
    okt 07, 2013 5:19:56 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
    ERROR: Table '`bibit`.`Book`' already exists
    okt 07, 2013 5:19:56 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
    INFO: HHH000230: Schema export complete
    Hibernate: insert into Book (createdDate, isbn, title) values (?, ?, ?)
    okt 07, 2013 5:19:56 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    WARN: SQL Error: 1146, SQLState: 42S02
    okt 07, 2013 5:19:56 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    ERROR: Table 'bibit.Book' doesn't exist
    Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
    	at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
    	at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
    	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2975)
    	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487)
    	at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    	at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
    	at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214)
    	at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
    	at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)
    	at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
    	at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
    	at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
    	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
    	at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
    	at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    	at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
    	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
    	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
    	at com.core.bibit.model.TestHibernate.main(TestHibernate.java:28)
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'bibit.Book' doesn't exist
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    	at com.mysql.jdbc.Util.getInstance(Util.java:386)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
    	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
    	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
    	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
    	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818)
    	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157)
    	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2460)
    	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2377)
    	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2361)
    	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
    	... 22 more

    And accoding to SQLDeveloper no table any idea?:
    Screen Shot 2013-10-07 at 17.21.10.png

    And my test code:
    package com.core.bibit.model;
     
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    import org.hibernate.service.ServiceRegistryBuilder;
     
    public class TestHibernate {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Book b = new Book();
    		b.setTitle("Harry Potter");
    		b.setIsbn("John");
     
    		Book a = new Book();
    		a.setTitle("Lort");
    		a.setIsbn("454-45-454321");
     
     
    		Configuration config = new Configuration();
    		config.configure();
    		ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
    		SessionFactory sessionFac = config.buildSessionFactory(sr);
    		Session session = sessionFac.openSession();
    		session.beginTransaction();
    		session.save(b);
    		//session.save(a);
    		session.getTransaction().commit();
     
    	}
     
    }

    My model class:
    package com.core.bibit.model;
     
    import java.util.Date;
     
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
     
     
    @Entity
    public class Book {
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private int id;
    	private String title, isbn;
    	private Date createdDate;
     
    	public Book(){
    		setCreatedDate(new Date());
    	}
     
    	public int getId(){
    		return id;
    	}
     
    	public void setId(int id){
    		this.id = id;
    	}
     
     
    	public String getTitle() {
    		return title;
    	}
     
    	public void setTitle(String title) {
    		this.title = title;
    	}
     
    	public String getIsbn() {
    		return isbn;
    	}
     
    	public void setIsbn(String isbn) {
    		this.isbn = isbn;
    	}
     
    	public Date getCreatedDate() {
    		return createdDate;
    	}
     
    	public void setCreatedDate(Date createdDate) {
    		this.createdDate = createdDate;
    	}
     
     
     
    }
    Ruby 'cause I can - Java 'cause I want - C# 'cause I have to

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Is Hibernate worth it?

    Hello,
    Check your hibernate configuration.
    You need to set up the hbm2ddl_auto property appropriately.

    Thanks,
    Syed.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Location
    Denmark
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Is Hibernate worth it?

    My Hibernate file:
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     
    <hibernate-configuration>
     
        <session-factory>
     
            <!-- Database connection settings -->
            <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
                        <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->
     
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:8889/bibit</property>
            <property name="connection.username">root</property>
            <property name="connection.password">root</property>
     
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
     
            <!-- SQL dialect -->
            <property name="dialect">
                org.hibernate.dialect.MySQLDialect
            </property>
     
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
     
            <!--<property name="cache.use_query_cache">true</property>-->
            <property name="cache.use_second_level_cache">true</property>
            <property name="cache.use_structured_entries">true</property>
            <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
            <property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property>
     
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
     
            <property name="hbm2ddl.auto">create</property>
     
            <mapping class="com.core.bibit.model.Book"/>
     
     
        </session-factory>
     
    </hibernate-configuration>

    Should this do it ?:
    <property name="hbm2ddl.auto">create</property>
    Ruby 'cause I can - Java 'cause I want - C# 'cause I have to

  6. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Is Hibernate worth it?

    Hi.
    I am sorry. Its hbm2ddl.auto property. You are right.
    Hibernate configuration seems to be ok. Can you show the mapping configuration?
    Also, can you print the entire stack trace if possible.

    Thanks,
    Syed.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Location
    Denmark
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Is Hibernate worth it?

    mapping is all so in the cof file, funny thing -> If I delete the database and create it again the code works for a couple of times then same error
    Ruby 'cause I can - Java 'cause I want - C# 'cause I have to

Similar Threads

  1. Replies: 2
    Last Post: January 12th, 2013, 12:16 PM
  2. Is it worth switching from PHP to JSP
    By mydarkpassenger in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: April 3rd, 2010, 02:23 AM