0

i have a problem using hibernate with my java project and i hope that you can help me:

First, here is some of my code:
The Entity class

package de.zaed.entities;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="USER")
public class User {

/**
 * the user id in the database
 */
@Id
private Long id; 

/**
 * the username of a user
 */
private String username;

/**
 * the users password
 */
private String password;

/**
 * The role of a user
 */
@OneToOne(mappedBy="user", cascade=CascadeType.ALL)
private UserRole role;


public Long getId() {
    return id;
}

public String getPassword() {
    return password;
}

public UserRole getRole() {
    return role;
}

public String getUsername() {
    return username;
}

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

public void setPassword(String password) {
    this.password = password;
}

public void setRole(UserRole role) {
    this.role = role;
}

public void setUsername(String username) {
    this.username = username;
}   
}

the part where i add the class to the AnnotationConfiguration

config.addAnnotatedClass(User.class); 
config.addAnnotatedClass(UserRole.class); 
config.configure("hibernate.cfg.xml");

now my problem:
while executing the following code:

User dbUser = new User();
dbUser.setUsername(username);
dbUser.setPassword(password);

Session session = DatabaseService.getSessionFactory().getCurrentSession();
session.beginTransaction();

session.save(dbUser);

session.flush();

session.close();

I get the

org.hibernate.MappingException: Unknown entity: de.zaed.entities.User 

error while session.save(dbUser) is called.

I've read many posts in forums and some tutorials but i could not find a fix for my problem. Hope you can help me.

Thanks, kaQn4p

kaQn4p
  • 1
  • 2

1 Answers1

0

There is no mapping for username and password in the User Class. You have to define annotation @Column e.g.

@Column(name = "username") private String username;

@Column(name = "password") private String password;

Assuming, the column defined in table User is same as the field defined in the User class. The column name is defined in name field of Column Annotation

Shashi
  • 12,487
  • 17
  • 65
  • 111
  • there is no need for you to define the column annotation, by default hibernate will use the variable name as the column name. – Sajan Chandran Jan 18 '13 at 12:26