Hibernate Entities generated using IntelliJ have no Foreign Keys generated

Answered

I have a database created in MariaDB called persistence with two related tables employee user, create a project with IntelliJ IDEA 2016.2.3 using Hibernate more JPA, achievement connect to the database but when making the mapping to the database and generate entities in these models have no foreign key relationship. But when revizar diagram IntelliJ Idea generated in the relationship if recognized. Someone can tell me is happening.

Adjunct source for error checking code.

https://www.dropbox.com/s/c2oqwc6vbu29xs7/JPAexample.zip?dl=0 

Thank you.

0
4 comments

Thanks for sample project. Works for me when I'm generating the models from my local MySQL database:

Database: MySQL (ver. 5.5.42)
Identifier case sensitivity: mixed (plain), upper (delimited)
Driver (JDBC4.0): MySQL Connector Java (ver. mysql-connector-java-5.1.35

Do you see foreign key constraints in Database tool window? Do you see them in Generate Persistence Mapping dialog (make sure you have check Show default relationship check box)? Are there any errors in idea.log when you are generating the mapping?

What database version and jdbc driver you use (please attach output from Test Connection button for the data source)?

 

 

0
Avatar
Permanently deleted user

The version of IntelliJ IDEA 2016.2.3

I have done all the steps that you describe and corrected some errors that had at the time of mapping, the problem I have now is that when you make an inquiry by the console JPA-Console throws me these warnings:

jpa-ql> select EmpleadoEntity.apellido from EmpleadoEntity
[09/09/2016 11:58:53] EmpleadoEntity is not mapped [select EmpleadoEntity.apellido from EmpleadoEntity]
jpa-ql> select EmpleadoEntity.apellido from employee
[09/09/2016 11:59:39] employee is not mapped [select EmpleadoEntity.apellido from employee]
jpa-ql> select EmpleadoEntity.apellido from models.EmpleadoEntity
[09/09/2016 12:01:04] models.EmpleadoEntity is not mapped [select EmpleadoEntity.apellido from models.EmpleadoEntity]
jpa-ql> select EmpleadoEntity.apellido from models.EmpleadoEntity
[09/09/2016 12:02:08] models.EmpleadoEntity is not mapped [select EmpleadoEntity.apellido from models.EmpleadoEntity]
jpa-ql> select EmpleadoEntity.apellido from EmpleadoEntity
[09/09/2016 12:03:55] EmpleadoEntity is not mapped [select EmpleadoEntity.apellido from EmpleadoEntity]
<code>

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>models.EmpleadoEntity</class>
<class>models.UsuarioEntity</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/persistencia"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="Mariana2803"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>

</code>

<code>

package models;

import javax.persistence.*;



/**
* Created by AFAL3D on 9/09/2016.
*/
@Entity
@Table(name = "empleado")
public class EmpleadoEntity {
private byte codEmpleado;
private String nombres;
private String apellido;
private String sexo;
private UsuarioEntity usuarioByCodEmpleado;

@Id
@Column(name = "codEmpleado")
public byte getCodEmpleado() {
return codEmpleado;
}

public void setCodEmpleado(byte codEmpleado) {
this.codEmpleado = codEmpleado;
}

@Basic
@Column(name = "nombres")
public String getNombres() {
return nombres;
}

public void setNombres(String nombres) {
this.nombres = nombres;
}

@Basic
@Column(name = "apellido")
public String getApellido() {
return apellido;
}

public void setApellido(String apellido) {
this.apellido = apellido;
}

@Basic
@Column(name = "sexo")
public String getSexo() {
return sexo;
}

public void setSexo(String sexo) {
this.sexo = sexo;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

EmpleadoEntity that = (EmpleadoEntity) o;

if (codEmpleado != that.codEmpleado) return false;
if (nombres != null ? !nombres.equals(that.nombres) : that.nombres != null) return false;
if (apellido != null ? !apellido.equals(that.apellido) : that.apellido != null) return false;
if (sexo != null ? !sexo.equals(that.sexo) : that.sexo != null) return false;

return true;
}

@Override
public int hashCode() {
int result = (int) codEmpleado;
result = 31 * result + (nombres != null ? nombres.hashCode() : 0);
result = 31 * result + (apellido != null ? apellido.hashCode() : 0);
result = 31 * result + (sexo != null ? sexo.hashCode() : 0);
return result;
}

@OneToOne
@JoinColumn(name = "codEmpleado", referencedColumnName = "codUsuario", nullable = false)
public UsuarioEntity getUsuarioByCodEmpleado() {
return usuarioByCodEmpleado;
}

public void setUsuarioByCodEmpleado(UsuarioEntity usuarioByCodEmpleado) {
this.usuarioByCodEmpleado = usuarioByCodEmpleado;
}
}
</code>

<code>

package models;

import javax.persistence.*;



/**
* Created by AFAL3D on 9/09/2016.
*/
@Entity
@Table(name = "usuario")
public class UsuarioEntity {
private byte codUsuario;
private String nombre;
private String clave;
private EmpleadoEntity empleadoByCodUsuario;

@Id
@Column(name = "codUsuario")
public byte getCodUsuario() {
return codUsuario;
}

public void setCodUsuario(byte codUsuario) {
this.codUsuario = codUsuario;
}

@Basic
@Column(name = "nombre")
public String getNombre() {
return nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

@Basic
@Column(name = "clave")
public String getClave() {
return clave;
}

public void setClave(String clave) {
this.clave = clave;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

UsuarioEntity that = (UsuarioEntity) o;

if (codUsuario != that.codUsuario) return false;
if (nombre != null ? !nombre.equals(that.nombre) : that.nombre != null) return false;
if (clave != null ? !clave.equals(that.clave) : that.clave != null) return false;

return true;
}

@Override
public int hashCode() {
int result = (int) codUsuario;
result = 31 * result + (nombre != null ? nombre.hashCode() : 0);
result = 31 * result + (clave != null ? clave.hashCode() : 0);
return result;
}

@OneToOne(mappedBy = "usuarioByCodEmpleado")
public EmpleadoEntity getEmpleadoByCodUsuario() {
return empleadoByCodUsuario;
}

public void setEmpleadoByCodUsuario(EmpleadoEntity empleadoByCodUsuario) {
this.empleadoByCodUsuario = empleadoByCodUsuario;
}
}

</code>

https://www.dropbox.com/s/c2oqwc6vbu29xs7/JPAexample.zip?dl=0 

 

0
Avatar
Permanently deleted user

Keep in mind that the pablabra HibernatePersistence org.hibernate.ejb.HibernatePersistence is crossed out and I do not understand why this?

0
Avatar
Permanently deleted user

That means it's deprecated. Use instead:

org.hibernate.jpa.HibernatePersistenceProvider
0

Please sign in to leave a comment.