Trouble creating entity with Hibernate/JPA

Asked

Viewed 673 times

2

Good afternoon guys, a little while ago I came across a problem in which I can’t find the solution. This is the following: I have 2 classes in the database created through annotations using JPA. However, I cannot create a third table. Below is the class of the two existing tables, and the class which I cannot create the table. When placing the @Entity and @id table tag the program itself does not run.

Clients class that generates the clients table in the BD

package model;

import javax.persistence.*;

import commons.SingleTon;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

@Entity
@Table (name="clients")
public class Client extends Model{

//Java Proprierties Headers
public static String NAME = "name";
public static String ADRESS = "adress";
public static String MAIL = "mail";
public static String PHONE = "phone";
public static String GENDER = "genderProperty";
public static String TYPE = "typeProperty";
public static String ID = "id";


@Id
@GeneratedValue
@Column (name="codCli")
public int getId() {
    return id.get();
}



public void setId(int id2) {
    id.set(id2);
}



public SimpleIntegerProperty idProperty(){
    return id;
}

public enum ClientHeader{
    cod("Codigo"),name("Nome"),adress("Endereco"),mail("Mail"),phone("Phone");

    String con;
    private ClientHeader(String con){
        this.con = con;
    }

    public String toString(){
        return con;
    }
}

protected SimpleStringProperty name = new SimpleStringProperty();
protected SimpleStringProperty adress = new SimpleStringProperty();
protected SimpleStringProperty mail = new SimpleStringProperty();
protected SimpleStringProperty phone = new SimpleStringProperty();
protected SimpleStringProperty genderProperty = new SimpleStringProperty();
protected SimpleStringProperty typeProperty = new SimpleStringProperty();
protected SimpleIntegerProperty id = new SimpleIntegerProperty();

private boolean hasComanda = false;

public Client(int id2) {
this.id = new SimpleIntegerProperty(this, ID, id2); 
}
public Client(){}
public Client(String name) {
    super();
    this.name = new SimpleStringProperty(this, NAME, name);
    adress = new SimpleStringProperty(this, ADRESS);
    mail = new SimpleStringProperty(this, MAIL);
    phone = new SimpleStringProperty(this, PHONE);
    this.genderProperty= new SimpleStringProperty(this, GENDER, "Masculino");
    this.typeProperty = new SimpleStringProperty(this, TYPE, "Cliente");
}

public Client(String name, String adress) {
    this(name);
    this.adress = new SimpleStringProperty(this, ADRESS, adress);
}

public Client(String name, String adress, String mail) {
    this(name, adress);
    this.mail = new SimpleStringProperty(this, MAIL, mail);
}

public Client(String name, String adress, String mail, String phone) {
    this(name, adress, mail);
    this.phone = new SimpleStringProperty(this, PHONE, phone);
}

public Client(String name, String adress, String mail, String phone
        , String gender) {
    this(name, adress, mail);
    this.phone = new SimpleStringProperty(this, PHONE, phone);
    genderProperty = new SimpleStringProperty(this, GENDER, gender);
}

public Client(String name, String adress, String mail, String phone
        , String gender, String type) {
    this(name, adress, mail, phone, gender);
    typeProperty = new SimpleStringProperty(this, TYPE, type);
}

public SimpleStringProperty nameProperty(){
    return name;
}

public SimpleStringProperty adressProperty(){
    return adress;
}

public SimpleStringProperty mailProperty(){
    return mail;
}

public SimpleStringProperty phoneProperty(){
    return phone;
}

public SimpleStringProperty genderProperty(){
    return genderProperty;
}

public SimpleStringProperty typeProperty(){
    return typeProperty;
}
@Column (name="nameCli", length=40)
public String getName() {
    return name.get();
}

@Column (name="addressCli", length=70)
public String getAdress() {
    return adress.get();
}

@Column (name="mailCli", length=50)
public String getMail() {
    return mail.get();
}

@Column (name="phoneCli", length=25)
public String getPhone() {
    return phone.get();
}

@Column (name="genderCli", length=20)
public String getGenderProperty(){
    return genderProperty.get();
}

@Column (name="typeCli", length=20)
public String getTypeProperty(){
    return typeProperty.get();
}
public void setGenderProperty(String a){
    genderProperty.set(a);
}

public void setTypeProperty(String a){
    typeProperty.set(a);
}


public static final class Types
{
    public static final ObservableList<String> getTypes(){
        ObservableList<String> types = FXCollections.observableArrayList();
        types.add("Cliente");
        types.add("Fornecedor");
        return types;
    }
}
public static final class Genders
{
    public static final ObservableList<String> getGenders(){
        ObservableList<String> gender = FXCollections.observableArrayList();
        gender.add("Masculino");
        gender.add("Feminino");
        return gender;
    }
}
public void setAdress(String adress) {
    if (this.adress != null){
        this.adress.set(adress);
    }
}

public void setMail(String mail) {
    this.mail.set(mail);
}


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

public void setPhone(String phone) {
    this.phone.set(phone);
}

void setComanda(boolean comanda){
    hasComanda = comanda;
}

public boolean hasComanda(){
    return hasComanda;
}


@Override
public String toString() {
    return (getName());
}

@Override
public void save() {
    /*
    EntityManager manager = SingleTon.getInstance().createEntityManager();
    manager.persist(this);
    manager.getTransaction().begin();
    manager.getTransaction().commit();
    manager.close();
    */
}

@Override
public void delete() {
    /*// TODO Auto-generated method stub
    EntityManager manager = SingleTon.getInstance().createEntityManager();
    Client c = manager.getReference(Client.class, this.getId());
    manager.remove(c);
    manager.getTransaction().begin();
    manager.getTransaction().commit();
    manager.close();
    */
}

@Override
public void update() {
    // TODO Auto-generated method stub

}



public void updated(Client p) {
    /*
    EntityManager manager = SingleTon.getInstance().createEntityManager();
    Client p2 = manager.merge(p);
    p2 = this;
    manager.getTransaction().begin();
    manager.getTransaction().commit();
    manager.close();
    */
}


}

Users class that generates the users table in the database

package model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import commons.Password;
import commons.SingleTon;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

@Entity
@Table (name="users")
public class User extends Model{

//Java Proprierties Headers
    public static String NAME = "name";
    public static String NIVEL = "nivel";
    public static String PASSWORD = "pass";
    public static String TIP = "tip";
    public static String ID = "id";

    protected SimpleStringProperty name = new SimpleStringProperty();
    protected SimpleStringProperty nivel = new SimpleStringProperty();
    protected SimpleStringProperty pass = new SimpleStringProperty();
    protected SimpleStringProperty tip = new SimpleStringProperty();
    protected SimpleIntegerProperty id = new SimpleIntegerProperty();
    @Id
    @GeneratedValue
    @Column (name="codUser", length=11) 
    public int getId() {
        // TODO Auto-generated method stub
        return id.get();
    }

    public void setId(int a)
    {
        this.id.set(a);
    }
    public SimpleIntegerProperty idProperty(){
        return id;
    }

    public User(){
        id = new SimpleIntegerProperty(this, ID);   
        this.name = new SimpleStringProperty(this, NAME);
        pass = new SimpleStringProperty(this, PASSWORD);
        nivel = new SimpleStringProperty(this, NIVEL);
    }

    public User(int id2) 
    {
        this.id = new SimpleIntegerProperty(this, ID, id2); 
    }

    public User(String name) {
        super();
        this.name = new SimpleStringProperty(this, NAME, name);
        //pass = new SimpleStringProperty(this, PASSWORD, Password.toHash("1234"));
        pass = new SimpleStringProperty(this, PASSWORD, "123213");
        nivel = new SimpleStringProperty(this, NIVEL, "Gerente");
        this.tip = new SimpleStringProperty(this, TIP, "1234");
    }

    public User(String name, String nivel) {
        this(name);
        this.nivel = new SimpleStringProperty(this, NIVEL, nivel);
    }

    public User(String name, String nivel, String pass) {
        this(name, nivel);
        //this.pass = new SimpleStringProperty(this, PASSWORD, Password.toHash(pass));
        this.pass = new SimpleStringProperty(this, PASSWORD, pass);
    }

    public User(String name, String nivel, String pass, String tip){
        this(name,nivel,pass);
        this.tip = new SimpleStringProperty(this, TIP, tip);
    }

    public SimpleStringProperty nameProperty(){
        return name;
    }

    public SimpleStringProperty niveleProperty(){
        return nivel;
    }

    public SimpleStringProperty passProperty(){
        return pass;
    }

    public SimpleStringProperty tipProperty(){
        return tip;
    }

    @Column (name="nameUser", length=25)
    public String getName() {
        return name.get();
    }

    @Column (name="passUser", length=255)
    public String getPass()
    {
        return pass.get();
    }

    @Column (name="nivelUser", length=25)
    public String getNivel()
    {
        return nivel.get();
    }

    @Column (name="tipUser", length=25)
    public String getTip(){
        return tip.get();
    }

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

    public void setPass(String pass)
    {
        this.pass.set(pass);
    }

    public void setNivel(String nivel)
    {
        this.nivel.set(nivel);
    }

    public void setTip(String tip){
        this.tip.set(tip);
    }

@Override
public void save() {
    // TODO Auto-generated method stub
    EntityManager manager = SingleTon.getInstance().createEntityManager();
    manager.persist(this);
    manager.getTransaction().begin();
    manager.getTransaction().commit();
    manager.close();

}

@Override
public void delete() {
    // TODO Auto-generated method stub
    EntityManager manager = SingleTon.getInstance().createEntityManager();
    User c = manager.getReference(User.class, this.getId());
    manager.remove(c);
    manager.getTransaction().begin();
    manager.getTransaction().commit();
    manager.close();

}

public void update(User p) {
    // TODO Auto-generated method stub
    EntityManager manager = SingleTon.getInstance().createEntityManager();
    User p2 = manager.merge(p);
    p2 = this;
    manager.getTransaction().begin();
    manager.getTransaction().commit();
    manager.close();

}

@Override
public void update() {
    // TODO Auto-generated method stub

}

}

Usercategory class, which for some reason I cannot generate in the database. After the code is the logcat of the application execution with the error. Just by placing annotations @Entity and @Id the error is already generated.

package model;

import javax.persistence.Entity;
import javax.persistence.Id;

import javafx.beans.property.SimpleStringProperty;


public class UserCategory extends Model{

    //Java Proprierties Headers
    public static String NAME = "name";
    public static String LEVEL = "level";

    public int getId() {
        // TODO Auto-generated method stub
        return super.getId();
    }

    public enum UserCategoryHeader{
        id("Codigo"),name("Nome"),level("Nivel");

        String con;
        private UserCategoryHeader(String con){
            this.con = con;
        }

        @Override
        public String toString(){
            return con;
        }
    }

    protected SimpleStringProperty name;
    protected SimpleStringProperty level;

    public UserCategory(){
        this.name = new SimpleStringProperty(this, NAME);
        this.level = new SimpleStringProperty(this, LEVEL);
    }

    public UserCategory(int id2) {
        super(id2);
    }

    public UserCategory(String name) {
        super();
        this.name = new SimpleStringProperty(this, NAME, name);
    }

    public UserCategory(String name, String level) {
        this(name);
        this.level = new SimpleStringProperty(this, LEVEL, level);
    }

    public SimpleStringProperty nameProperty(){
        return name;
    }

    public SimpleStringProperty levelProperty(){
        return level;
    }

    public String getName() {
        return name.get();
    }

    public String getLevel() {
        return level.get();
    }

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

    public void setAdress(String adress) {
        this.level.set(adress);
    }

    @Override
    public String toString() {
        return ("\nId: " + getId() + " Name: " + getName() + " ");
    }

    @Override
    public void save() {
        // sql de insert no bd  
    }

    @Override
    public void delete() {
        // TODO Auto-generated method stub  
    }

    @Override
    public void update() {
        // TODO Auto-generated method stub

    }

}

Persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="casa" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/casadochopp" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="root" />

        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="false" />
        <property name="hibernate.use_sql_comments" value="false" />
        <property name="hibernate.jdbc.wrap_result_sets" value="false" />
        <property name="hibernate.hibernate.cache.use_query_cache" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="update" />

    </properties>

</persistence-unit>

SYSTEM LOG working without trying to edit the Usercategory class

Sep 10, 2014 3:39:26 pm org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. Sep 10, 2014 3:39:26 pm org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. Sep 10, 2014 3:39:26 pm org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. Sep 10, 2014 3:39:26 PM org.hibernate.jpa.internal.util.Loghelper logPersistenceUnitInformation INFO: HHH000204: Persistenceunitinfo Processing [ name: house ...] Sep 10, 2014 3:39:26 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.6.Final} Sep 10, 2014 3:39:26 pm org.hibernate.cfg.Environment INFO: HHH000206: Hibernate.properties not found Sep 10, 2014 3:39:26 pm org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode Provider name : javassist Sep 10, 2014 3:39:26 PM org.hibernate.Annotations.common.Reflection.java.Javareflectionmanager INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final} Sep 10, 2014 3:39:26 PM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure WARN: HHH000402: Using Hibernate built-in Connection pool (not for Production use!) Sep 10, 2014 3:39:26 PM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/casadochopp] Sep 10, 2014 3:39:26 PM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH000046: Connection properties: {user=root, password=***} Sep 10, 2014 3:39:26 PM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH000006: Autocommit mode: false Sep 10, 2014 3:39:26 PM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure INFO: HHH000115: Hibernate Connection pool size: 20 (min=1) Sep 10, 2014 3:39:27 pm org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.Mysql5innodbdialect Sep 10, 2014 3:39:27 PM org.hibernate.hql.internal.Ast.Astquerytranslatorfactory INFO: HHH000397: Using Astquerytranslatorfactory Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Schemaupdate execute INFO: HHH000228: Running hbm2ddl schema update Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Schemaupdate execute INFO: HHH000102: Fetching database Metadata Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Schemaupdate execute INFO: HHH000396: Updating schema Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Tablemetadata INFO: HHH000261: Table found: casadochopp.clients Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Tablemetadata INFO: HHH000037: Columns: [typecli, phonecli, codcli, addresscli, namecli, gendercli, mailcli] Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Tablemetadata INFO: HHH000108: Foreign Keys: [] Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Tablemetadata INFO: HHH000126: Indexes: [Primary] Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Tablemetadata INFO: HHH000261: Table found: casadochopp.users Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Tablemetadata INFO: HHH000037: Columns: [nameuser, passuser, tipuser, coduser, niveluser] Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Tablemetadata INFO: HHH000108: Foreign Keys: [] Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Tablemetadata INFO: HHH000126: Indexes: [Primary] Sep 10, 2014 3:39:28 PM org.hibernate.tool.hbm2ddl.Schemaupdate execute INFO: HHH000232: Schema update complete

Now, the edited Usercategory class, in an attempt to add the table to the database

package model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

@Entity

public class UserCategory extends Model{

    //Java Proprierties Headers
    public static String NAME = "name";
    public static String LEVEL = "level";
    public static String ID = "id";

    @Id
    @GeneratedValue
    public int getId() {
        return this.id.get();
    }
    public void setId(int d) {
        // TODO Auto-generated method stub
        this.id.set(d);
    }
    public SimpleIntegerProperty idProperty(){
        return id;
    }

    public enum UserCategoryHeader{
        id("Codigo"),name("Nome"),level("Nivel");

        String con;
        private UserCategoryHeader(String con){
            this.con = con;
        }

        @Override
        public String toString(){
            return con;
        }
    }

    protected SimpleStringProperty name = new SimpleStringProperty(this, NAME);
    protected SimpleStringProperty level = new SimpleStringProperty(this, LEVEL);
    protected SimpleIntegerProperty id = new SimpleIntegerProperty(this, ID);



    public UserCategory(){}

    public UserCategory(int id2) {
        this.id =  new SimpleIntegerProperty(this, ID);
    }

    public UserCategory(String name) {
        super();
        this.name = new SimpleStringProperty(this, NAME, name);
    }

    public UserCategory(String name, String level) {
        this(name);
        this.level = new SimpleStringProperty(this, LEVEL, level);
    }

    public SimpleStringProperty nameProperty(){
        return name;
    }

    public SimpleStringProperty levelProperty(){
        return level;
    }

    public String getName() {
        return name.get();
    }

    public String getLevel() {
        return level.get();
    }

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

    public void setAdress(String adress) {
        this.level.set(adress);
    }

    @Override
    public String toString() {
        return ("\nId: " + getId() + " Name: " + getName() + " ");
    }

    @Override
    public void save() {
        // sql de insert no bd  
    }

    @Override
    public void delete() {
        // TODO Auto-generated method stub  
    }

    @Override
    public void update() {
        // TODO Auto-generated method stub

    }

}

LOG after class editing and compilation attempt

Sep 10, 2014 3:36:54 pm org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. Sep 10, 2014 3:36:54 pm org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. Sep 10, 2014 3:36:54 pm org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. Sep 10, 2014 3:36:54 PM org.hibernate.jpa.internal.util.Loghelper logPersistenceUnitInformation INFO: HHH000204: Persistenceunitinfo Processing [ name: house ...] Sep 10, 2014 3:36:54 pm org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.6.Final} Sep 10, 2014 3:36:54 pm org.hibernate.cfg.Environment INFO: HHH000206: Hibernate.properties not found Sep 10, 2014 3:36:54 pm org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode Provider name : javassist Sep 10, 2014 3:36:55 PM org.hibernate.Annotations.common.Reflection.java.Javareflectionmanager INFO: HCANN000001: Hibernate Commons Annotations

  • 1

    Look at the first 5 lines of the stacktrace, the message is very clear. How is the setup of your application ? ta running on top of an application server ? what versions of libs are being used ? Other than that, your Pojos are very confused, Hibernate needs them to be at the convention.

  • This logcat even appears with the application in perfect running state. Below is an example of logcat in operation.

  • Edited question, now I think you can understand better.

  • Usercategory must have @Entity for Hibernate to recognize. As for the original error, it seems to me the wrong version of your jars of Hibernate: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.

  • Why logcat is used ?

  • Look, I honestly didn’t see anything unusual in your log. Did you happen to try @Name? Another strange thing was this Enum: Why don’t you use the @Enumerated annotation?

  • The tag name you refer to Column (name="xx") is that it? I believe that Enum is not the problem because it exists in the other classes too, I can not find the error... because the others are practically identical.

  • Guys thanks for the help, however I found the solution of poblema. It was missing the Setter of the variable level.

Show 3 more comments

1 answer

1

To solve the problem described below: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.,

go to your persistence.xml file and change the persientence Provider

of <provider>org.hibernate.ejb.HibernatePersistence</provider>

for <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.