晚上好人!
背景:正如我在第一个问题(两天前)中所说,我是Hibernate的新人。 在最后一个问题中,人们建议我使用Maven更好地利用依赖性。我做到了,我很高兴。
我认为我已经完成了(几乎)所有我必须做的工作来完成Hibernate项目。我遇到的问题是我不能坚持所有实体。
控制台告诉我:
dic 20, 2016 12:21:34 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.5.Final}
dic 20, 2016 12:21:34 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
dic 20, 2016 12:21:34 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
dic 20, 2016 12:21:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
dic 20, 2016 12:21:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/vinoteca]
dic 20, 2016 12:21:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
dic 20, 2016 12:21:34 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
dic 20, 2016 12:21:34 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Tue Dec 20 00:21:34 CET 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
dic 20, 2016 12:21:34 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
dic 20, 2016 12:21:35 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
dic 20, 2016 12:21:35 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/vinoteca]
dic 20, 2016 12:21:35 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
dic 20, 2016 12:21:35 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
dic 20, 2016 12:21:35 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Tue Dec 20 00:21:35 CET 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
dic 20, 2016 12:21:35 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: integracion.entidades.Bodega
at org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:637)
at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2920)
at org.hibernate.internal.SessionImpl.access$1800(SessionImpl.java:204)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2671)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2657)
at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1185)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:1058)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:355)
at com.sun.proxy.$Proxy18.get(Unknown Source)
at integracion.dao.GenericDAOImpl.get(GenericDAOImpl.java:48)
at negocio.Main.main(Main.java:32)
我的实体就是这些:
package integracion.entidades;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Table;
@DynamicUpdate
@Table(appliesTo = "bodega")
public class Bodega implements Serializable {
@Id
@Column(name="id")
private int id;
@Column(name="nombre")
private String nombre;
@OneToMany(mappedBy="bodega",cascade= CascadeType.ALL)
private Set<Vino> vinos;
public Bodega(){
}
public Bodega(int id, String nombre) {
this.id = id;
this.nombre = nombre;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
}
package integracion.entidades;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Table;
@DynamicUpdate
@Table(appliesTo = "Denominacion")
public class Denominacion implements Serializable{
@Id
@Column(name="id")
private int id;
@Column(name="nombre")
private String nombre;
public Denominacion(){
}
public Denominacion(int id, String nombre) {
this.id = id;
this.nombre = nombre;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
}
package integracion.entidades;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Table;
@DynamicUpdate
@Table(appliesTo = "vino")
public class Vino implements Serializable{
@Id
@Column(name="id")
private int id;
@ManyToOne
@JoinColumn(name="id_bodega")
private int id_bodega;
@ManyToOne
@JoinColumn(name="id_denominacion")
private int id_denominacion;
@Column(name="nombre")
private String nombre;
@Column(name="anho")
private int anho;
/*
* <many-to-one name="bodega">
<column name="id_bodega" />
</many-to-one>
*/
public Vino(){
}
public Vino(int id, int id_bodega, int id_denominacion, String nombre, int anho) {
this.id = id;
this.id_bodega = id_bodega;
this.id_denominacion = id_denominacion;
this.nombre = nombre;
this.anho = anho;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getId_bodega() {
return id_bodega;
}
public void setId_bodega(int id_bodega) {
this.id_bodega = id_bodega;
}
public int getId_denominacion() {
return id_denominacion;
}
public void setId_denominacion(int id_denominacion) {
this.id_denominacion = id_denominacion;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public int getAnho() {
return anho;
}
public void setAnho(int anho) {
this.anho = anho;
}
}
我的映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/vinoteca</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping class="src.main.java.integracion.entidades.Bodega.java"/>
<mapping class="src.main.java.integracion.entidades.Denominacion.java"/>
<mapping class="src.main.java.integracion.entidades.Vino.java"/>
</session-factory>
</hibernate-configuration>
这里我有树项目:
我有两个理论:
首先,问题可能是hibernate.cfg.xml导致(可能)映射类标记的路径错误。
第二,也许注释实体是错误的,因为我写错了关系(1:1,1:n,n:N)。
伙计们,你给我的所有帮助我谢谢你!
问候!