Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6725177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:46:35+00:00 2026-05-26T09:46:35+00:00

I am new to Hibernate. While creating a small app using it I got

  • 0

I am new to Hibernate. While creating a small app using it I got following exception:

Exception in thread “main” java.lang.IllegalArgumentException: Unknown entity:
model.Students at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:223)
at controller.Main.main(Main.java:50)

Can anybody please help me out?

The Entity classes are as follows:

Other details:
NetBeans Version: 6.7.1  
Hibernate : 3.2.5

Entity Students

package model;  
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Students implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @OneToOne(cascade=CascadeType.ALL)
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

Another Entity Class

package model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;  
@Entity

public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    private String city;


    private String zip;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

The DAO file

package controller;  import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import model.Address;
import model.Students;
import org.hibernate.HibernateException;
public class Main {
    public static void main(String arr[])
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneToOne2PU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tr= em.getTransaction();
        try{

            tr.begin();

            Address add1 = new Address();
            add1.setCity("pune");
            add1.setZip("09");
            Address add2 = new Address();
            add2.setCity("mumbai");
            add2.setZip("12");

            Students s1 = new Students();
            s1.setName("abc");
            s1.setAddress(add1);
            Students s2 = new Students();
            s2.setName("xyz");
            s2.setAddress(add2);

            em.persist(s1);
            em.persist(s2);

            tr.commit();
            emf.close();

        }
        catch(HibernateException e){
            e.printStackTrace();

        }
    }

}

The persistence.xml

<?xml version="1.0" encoding="UTF-8"?>    
<persistence version="1.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_1_0.xsd">    
  <persistence-unit name="OneToOnePU" transaction-type="JTA">    
    <provider>org.hibernate.ejb.HibernatePersistence</provider>    
    <jta-data-source>students</jta-data-source>    
    <exclude-unlisted-classes>false</exclude-unlisted-classes>    
    <properties>    
      <property name="hibernate.hbm2ddl.auto" value="create-tables"/>    
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>    
      <property name="hibernate.connection.username" value="app"/>    
      <property name="hibernate.connection.password" value="app"/>    
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>    
    </properties>    
  </persistence-unit>   
</persistence>

The hibernate.cfg.xml 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>  
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>  
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>  
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property>    
    <property name="hibernate.connection.username">app</property>  
    <property name="hibernate.connection.password">app</property>  
    <mapping class="model.Students"/>  
    <mapping class="model.Address"/>  
  </session-factory>  
</hibernate-configuration> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:46:36+00:00Added an answer on May 26, 2026 at 9:46 am

    Depends bit about project structure, but likely by adding following to persistence.xml directly under persistence-unit element.

    <class>model.Students</class>
    <class>model.Address</class>
    

    Exactly like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.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_1_0.xsd">
      <persistence-unit name="OneToOnePU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
        <jta-data-source>students</jta-data-source>
        <class>model.Students</class>
        <class>model.Address</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
          <property name="hibernate.hbm2ddl.auto" value="create-tables"/>
          <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
          <property name="hibernate.connection.username" value="app"/>
          <property name="hibernate.connection.password" value="app"/>
          <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>
        </properties>
      </persistence-unit>
    </persistence>
    

    By the way, why do you configure properties like hibernate.dialect in both persistence.xml and hibernate.cfg.xml?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using Hibernate with Java. I'm getting the following exception. The stuff
I am new to Hibernate. I have an exception while running an Hibernate-based application.
I'm new to hibernate. While writing hibernate code, I found that there are following
I am new to Hibernate and have the following situation: class A class Course
I'm getting exception while creating connection with SQL Server. ISession factory is shown below
I'm new to Hibernate, and while there are literally tons of examples to look
While fetching Boolean data from database with hibernate SQL query gives the following error
i am getting exception while the server starts. (Server is started using Intelij IDE).
I am new to Spring and hibernate please help, I am using (Jboss 6.0
I am new to Hibernate. We are migrating one of our existing web application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.