Results 1 to 5 of 5
  1. #1
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Why??? -> org.hibernate.MappingException: Unknown entity: main.java.table.Book

    Hi, everyone!
    I am from Ukraine. Sorry, but my english is not very good. Hope, you are understand my text.
    My story. Just start learning Hibernate, and got exception on first sample. Why?

    P.S. Is any one can recommend a good books? I've got "3rd edition. J.Ottinger, D.Minter, J.Linwood Beginning Hibernate."

    First - output to console, my code follows below:

    Hi there!
    Let's save!

    Exception in thread "main" org.hibernate.MappingException: Unknown entity: main.java.table.Book
    at org.hibernate.internal.SessionFactoryImpl.getEntit yPersister(SessionFactoryImpl.java:776)
    at org.hibernate.internal.SessionImpl.getEntityPersis ter(SessionImpl.java:1447)
    at org.hibernate.event.internal.AbstractSaveEventList ener.saveWithGeneratedId(AbstractSaveEventListener .java:100)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEv entListener.saveWithGeneratedOrRequestedId(Default SaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListe ner.saveWithGeneratedOrRequestedId(DefaultSaveEven tListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEv entListener.entityIsTransient(DefaultSaveOrUpdateE ventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListe ner.performSaveOrUpdate(DefaultSaveEventListener.j ava:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEv entListener.onSaveOrUpdate(DefaultSaveOrUpdateEven tListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(Sessio nImpl.java:678)
    at org.hibernate.internal.SessionImpl.save(SessionImp l.java:670)
    at org.hibernate.internal.SessionImpl.save(SessionImp l.java:665)
    at main.java.table.DemoFirst.main(DemoFirst.java:25)
    My code:
    -------------------Book.java
    Java Code:
    package main.java.table;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    
    @Entity
    @Table(name="book")
    public class Book {
    	@Id
    	private int id;
    	@Column(name="title")
    	private String title;
    	@Column(name="description")
    	private String description;
    	@Column(name="isbn")
    	private int isbn;
    
    	public Book(){}; 
    	
    	public void setBook(int id, String title, String description, int isbn) {		
    		this.id = id;
    		this.title = title;
    		this.description = description;
    		this.isbn = isbn;
    	}
    
    	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 getDescription() {
    		return description;
    	}
    
    	public void setDescription(String description) {
    		this.description = description;
    	}
    
    	public int getIsbn() {
    		return isbn;
    	}
    
    	public void setIsbn(int isbn) {
    		this.isbn = isbn;
    	}
    
    	@Override
    	public String toString() {
    		return "Book [id=" + id + ", title=" + title + ", description=" + description + ", isbn=" + isbn + "]";
    	}
    
    }
    -------------------DemoFirst.java:
    Java Code:
    package main.java.table;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    
    public class DemoFirst {
    
    	public static void main(String[] args) {
    
    		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    		Session session = sessionFactory.openSession();
    		session.beginTransaction();
    
    		System.out.println("Hi there!");
    
    		Book book = new Book();
    
    		book.setBook(1, "Title", "Description", 12);
    
    		System.out.println("Let's save!");
    
    		session.save(book);                  // Exception throws from here! But why?
    		session.getTransaction().commit();
    
    		session.close();
    
    	}
    }
    ------------------HibernateUtil.java
    Java Code:
    package main.java.table;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;  
    import org.hibernate.service.ServiceRegistry; 
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
      
    public class HibernateUtil {  
          
        private static SessionFactory factory;  
        private static ServiceRegistry sr;  
          
        static {  
        	Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();        
            factory = configuration.buildSessionFactory(sr);
        	 	
        }  
          
        public static SessionFactory getSessionFactory() {  
            return factory;  
        }  
    }
    -------------------hibernate.cfg.xml

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
      
    <hibernate-configuration>  
        <session-factory>  
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
            <property name="hibernate.connection.username">root</property>  
            <property name="hibernate.connection.password"></property>  
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstore</property>  
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
            <property name="show_sql">true</property>   
            <property name="connection.pool_size">1</property>  
              
            <mapping class="main.java.table.Book"></mapping>  
      
        </session-factory>  
    </hibernate-configuration>
    Last edited by Max0314; 09-16-2015 at 09:25 AM.

  2. #2
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why??? -> org.hibernate.MappingException: Unknown entity: main.java.table.Boo

    People! I had solve problem, but nothing to understand.

    I was replace code that retrieved sessionFactory.

    Was:
    Java Code:
        	Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();        
            factory = configuration.buildSessionFactory(sr);
    Now(it's from https://docs.jboss.org/hibernate/orm...basic-entity):

    Java Code:
            factory = new Configuration()
                    .configure() // configures settings from hibernate.cfg.xml
                    .buildSessionFactory();
    Or(also work, I get it from tutorial Hibernate Getting Started Guide ):

    Java Code:
        	// A SessionFactory is set up once for an application!
         	final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
         			.configure() // configures settings from hibernate.cfg.xml
         			.build();
         	try {
         		factory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
         	}
         	catch (Exception e) {
         		// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
         		// so destroy it manually.
         		StandardServiceRegistryBuilder.destroy( registry );
         	}

    But still don't understand what the problem was?
    I need a good book about Hibernate!

  3. #3
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why??? -> org.hibernate.MappingException: Unknown entity: main.java.table.Boo

    Intrested. Does any one says hello to me, at least? (:

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Why??? -> org.hibernate.MappingException: Unknown entity: main.java.table.Boo

    Hello!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Max0314 is offline Member
    Join Date
    Sep 2015
    Posts
    15
    Rep Power
    0

    Default Re: Why??? -> org.hibernate.MappingException: Unknown entity: main.java.table.Boo

    Hi!

    Thanks. I was start to suppose, no one real human present here. (:

Similar Threads

  1. org.hibernate.MappingException: invalid configuration?
    By blertadhimitri in forum New To Java
    Replies: 6
    Last Post: 01-30-2014, 12:10 PM
  2. Replies: 1
    Last Post: 09-11-2013, 02:48 PM
  3. hibernate unknown column
    By arpho in forum New To Java
    Replies: 0
    Last Post: 10-15-2011, 11:27 AM
  4. Replies: 1
    Last Post: 06-21-2011, 12:50 PM
  5. Replies: 4
    Last Post: 05-13-2011, 10:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •