0

I receiving this error all time when I try to save a new "user". I hane no problem when I list the "users" recorded in my database "test"

This is my hibernate.cfg.xml:

<?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.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">false</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping class="test.model.Users"/>
  </session-factory>
</hibernate-configuration>

This is my Users.java:

package test.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
@Table(name="users")
public class Users implements Serializable {

    private static final long serialVersionUID = 1L;
    private Integer id;
    private String login;
    private String name;
    private String password;
    private String access;
    private String status;

    public Users() {
    }

    public Users(String login, String name, String password, String access, String status) {
        this.login = login;
        this.name = name;
        this.password = password;
        this.access = access;
        this.status = status;
    }

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

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

    @Column(name="login", nullable=false, length=20)
    public String getLogin() {
        return this.login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

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

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

    @Column(name="password", nullable=false, length=50)
    public String getPassword() {
        return this.password;
    }

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

    @Column(name="access", nullable=false, length=15)
    public String getAccess() {
        return this.access;
    }

    public void setAccess(String access) {
        this.access = access;
    }

    @Column(name="status", nullable=false, length=10)
    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

Where I wrong my friends ?

1 Answer 1

1

Probably the error is happening because of this:

<mapping class="test.Users"/>

As I could see, your 'Users' class is inside the 'test.model' package, and not just 'test'

Try to change your mapping by the following:

<mapping class="test.model.Users"/>

Good luck

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

5 Comments

Hi N0nbot. Sorry, I put the mapping wrong here. I fix the ask.
Realized that you are import the Persistence libraries, but are using the hibernate.cfg.xml as settings. Don't you use a 'persistence.xml' file in your project? If so, try to map your class there. If not, then import the hibernate packages instead of persistence ones.
N0nbot, I´m newbie. I´m not using persistence.xml. What I do ?
Can I remove "mapping class" from hibernate.cfg.xml and create a persistence.xml ?
Persistence.xml is a configuration file to use the JPA (Java Persistence API), that works pretty like hibernate. I can see that your project already have the javax.persistence packages, so you gonna be able to use it. Add in your META-INF folder a new "Persistence Unit" file, and then configure it's source (in the tab "Source", instead of "Design") with your properties and mappings used in the hibernate.cfg.xml. These properties and mapping should be declared inside the "persistence-unit" tag. If you face any problems configuring it, try to do some we research about "Persistence Unit Settings"

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.