2

I have written few codes for inserting data into my SQL database. Actually, I am trying to learn Struts 2 with Hibernate. But, unfortunately I am facing a problem after submitting my form.

I could not find the reason for this error message. My try & catch block throws error like:

Exception in saveOrUpdate() Rollback  :org.hibernate.MappingException: Unknown entity: v.esoft.pojos.Employee

Pojo(Employee.java):

@Entity
@Table(name = "employee", catalog = "eventusdb")
public class Employee implements java.io.Serializable {

    private Integer empId;
    private String name;
    private String website;

    public Employee() {
    }

    public Employee(String name, String website) {
        this.name = name;
        this.website = website;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "emp_id", unique = true, nullable = false)
    public Integer getEmpId() {
        return this.empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return this.name;
    }

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

    @Column(name = "website", nullable = false, length = 65535)
    public String getWebsite() {
        return this.website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

}

also having Employee.hbm.xml:

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 10, 2013 4:29:04 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="v.esoft.pojos.Employee" table="employee" catalog="eventusdb">
        <id name="empId" type="java.lang.Integer">
            <column name="emp_id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" not-null="true" />
        </property>
        <property name="website" type="string">
            <column name="website" length="65535" not-null="true" />
        </property>
    </class>
</hibernate-mapping>
4
  • which line no. you are getting this exception? Commented Sep 10, 2013 at 11:59
  • @Aniket that only i don't know? how to know which line i am getting exception? Commented Sep 10, 2013 at 12:06
  • 1
    Use e.printStackTrace(); in addition to your System.out.println(). And for the future, use Log4J or something similar, that will parse the Exception's StackTrace for you. Commented Sep 10, 2013 at 12:37
  • @AndreaLigios This problem solved actually i have to map my POJO class, But after running this code its not inserting my form data into the table. I am getting 1 row inserted successfully. message but no insertion of data in my table. Commented Sep 11, 2013 at 6:39

1 Answer 1

0

If the entity isn't mapped, you should inspect the hibernate configuration. Hibernate is ORM framework used to map pojos (entities) to the database schema objects. You didn't configure or hibernate couldn't find mapping for the object Employee. The configuration file is hibernate.cfg.xml should contain the mapping to the resource Employee.hbm.xml. Suppose this file is in the same folder as Employee class. Then the mapping will be

<mapping resource="v/esoft/pojos/Employee.hbm.xml"/>

Another approach if you used an annotation based configuration, then you should use class attribute to map to the pojo that contains Hibernate/JPA annotations.

<mapping class="v.esoft.pojos.Employee"/>

Note, annotation based Configuration might be different depending on version of Hibernate and may require additional libraries.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks i added <mapping class="v.esoft.pojos.Employee"/> in my hibernate.cfg file and now this problem is solved. But Now its not inserting into my database code. Its not showing any error for this error. What could be the mistakes?
It could be a transaction that you are not committing, or database related.
Thanks this problem has been solved. May be you can help me with this one stackoverflow.com/questions/18742091/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.