更改表结构但hibernate不更新它

时间:2016-04-13 18:35:12

标签: java hibernate postgresql

我正在使用Hibernate将数据持久化到postgres中的两个表,最近我在表结构中做了一些更改,我决定创建另一个数据库,当然我总是使用Postgres,当我尝试运行我发现的java代码时hibernate总是在寻找旧的表结构;可以肯定的是,我决定删除DBMS中的旧表,但是hibernate仍在寻找旧的表结构,事实上,我注意到了这一点,因为在最新的设计中不再存在字段。我的问题是:有没有办法在Hibernate中更新它?我应该在哪里寻找?我正在使用Eclipse Mars,我清理我的项目并重新启动它但仍然是相同的。

这是我的hibernate.cfg.xml

enter code here
<?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>
<!--  Database connection settings -->
<property name="connection.driver.class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/cineticket2</property>
<property name="connection.username">ok</property>
<property name="connection.password">ok123$</property>

<!-- SQL Dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<mapping class="sv.edu.ucad.et1.cineticket.data.entities.Usuarios"/>
<mapping class="sv.edu.ucad.et1.cineticket.data.entities.Cargos"/>  
<mapping class="sv.edu.ucad.et1.cineticket.data.entities.Departamentos"/>   

</session-factory>
</hibernate-configuration>

我的HibernateUtil.java是:

package sv.edu.ucad.et1.cineticket.data;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import sv.edu.ucad.et1.cineticket.data.entities.Cargos;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
    try{
        //Configuration configuration = new Configuration();
        //return configuration.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
        StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
        Metadata mdata = new MetadataSources(ssr).getMetadataBuilder().build();
        return mdata.getSessionFactoryBuilder().build();
    }catch(Exception e){
        e.printStackTrace();
        throw new RuntimeException("Ocurrio un error en la construcction de la Sesion Factory");
    }
}//fin de buildSessionfactory

public static SessionFactory getSessionFactory(){
    return sessionFactory;
}

}//fin de HibernateUitl

这是我的Departamentos.java

package sv.edu.ucad.et1.cineticket.data.entities;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name="departamentos")
@Access(value=AccessType.PROPERTY) //acceso a traves de getters

public class Departamentos {
private Long coddep;
private String nomdep;
private String desdep;
private boolean estdep;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="coddep", updatable=false)     
public Long getCoddep() {
    return coddep;
}
public void setCoddep(Long coddep) {
    this.coddep = coddep;
}

@Column(name="nomdep", nullable=false)      
public String getNomdep() {
    return nomdep;
}
public void setNomdep(String nomdep) {
    this.nomdep = nomdep;
}

@Column(name="desdep", nullable=false)      
public String getDesdep() {
    return desdep;
}
public void setDesdep(String desdep) {
    this.desdep = desdep;
}

@Column(name="estdep", nullable=false)      
public boolean isEstdep() {
    return estdep;
}
public void setEstdep(boolean estdep) {
    this.estdep = estdep;
}
}

这是我的Usuarios.java

package sv.edu.ucad.et1.cineticket.data.entities;
import java.util.Date;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="usuarios")
@Access(value=AccessType.PROPERTY) //acceso a traves de getters
public class Usuarios {

private Long codusu;
private String apeusu;
private String nomusu;
private String celusu;
private String dirusu;
private Date fcousu;
private String cueusu;
private String clausu;
private Long codsuc;
private Long codcar;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="coddep")
public Departamentos deptos;    

@Transient
public Departamentos getDeptos() {
    return deptos;
}
public void setDeptos(Departamentos deptos) {
    this.deptos = deptos;
}

//propiedad bandera, que se declara como @Transient
private boolean estado;

@Transient  
public boolean isEstado() {
    return estado;
}
public void setEstado(boolean estado) {
    this.estado = estado;
}


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="codusu", updatable=false) 
public Long getCodusu() {
    return codusu;
}
public void setCodusu(Long codusu) {
    this.codusu = codusu;
}

@Column(name="apeusu", nullable=false)      
public String getApeusu() {
    return apeusu;
}
public void setApeusu(String apeusu) {
    this.apeusu = apeusu;
}

@Column(name="nomusu", nullable=false)      
public String getNomusu() {
    return nomusu;
}
public void setNomusu(String nomusu) {
    this.nomusu = nomusu;
}

@Column(name="celusu", nullable=false)      
public String getCelusu() {
    return celusu;
}
public void setCelusu(String celusu) {
    this.celusu = celusu;
}

@Column(name="dirusu")      
public String getDirusu() {
    return dirusu;
}
public void setDirusu(String dirusu) {
    this.dirusu = dirusu;
}

@Column(name="cueusu", nullable=false)      
public String getCueusu() {
    return cueusu;
}
public void setCueusu(String cueusu) {
    this.cueusu = cueusu;
}

@Column(name="clausu", nullable= false)     
public String getClausu() {
    return clausu;
}
public void setClausu(String clausu) {
    this.clausu = clausu;
}

public Long getCodsuc() {
    return codsuc;
}
public void setCodsuc(Long codsuc) {
    this.codsuc = codsuc;
}   

@Column(name="codsal", nullable=false)      
public Long getCodsal() {
    return codsuc;
}
public void setCodsal(Long codsal) {
    this.codsuc = codsal;
}

@Column(name="codcar", nullable=false)  
public Long getCodcar() {
    return codcar;
}
public void setCodcar(Long codcar) {
    this.codcar = codcar;
}

@Column(name="fcousu")  
public Date getFcousu() {
    return fcousu;
}
public void setFcousu(Date fcousu) {
    this.fcousu = fcousu;
}

}//fin de Usuarios

这是主要类:

package sv.edu.ucad.et1.cineticket.data;

import java.util.Date;
import org.hibernate.Session;
import sv.edu.ucad.et1.cineticket.data.entities.Departamentos;
import sv.edu.ucad.et1.cineticket.data.entities.Usuarios;
public class UnoaMuchosDemo {
public static void main(String[] args){
    Session sesion = HibernateUtil.getSessionFactory().openSession();

    //inicio de la transaccion
    try{
        org.hibernate.Transaction transaccion = sesion.beginTransaction();

        Departamentos deptos = createNewDepartamentos();
        Usuarios usuarios = createNewUsuarios(deptos);

        sesion.save(usuarios);
        transaccion.commit();

    }catch(Exception e){
        e.printStackTrace();
    }finally{
        sesion.close();
        HibernateUtil.getSessionFactory().close();
    }

}

//clases empotradas que crean usuarios y deptos
//crea un nuevo usuario

private static Usuarios createNewUsuarios(Departamentos deptos) {
    Usuarios nusu = new Usuarios();
    nusu.setApeusu("Messi");
    nusu.setNomusu("Lionel");
    nusu.setCelusu("7588-8888");
    nusu.setDirusu("Camp Nou, Barcelona, Catalunya");
    nusu.setFcousu(new Date());
    nusu.setCueusu("messi@barcelona.com");
    nusu.setClausu("12345");
    nusu.setDeptos(deptos);
    nusu.setCodcar((long) 1);
    nusu.setCodsuc((long) 1);       
    return nusu;
}

//crea un nuevo depto
private static Departamentos createNewDepartamentos() {
    Departamentos ndepto = new Departamentos();
    ndepto.setNomdep("Finanzas");
    ndepto.setDesdep("Contabilidad, Tesoreria");
    ndepto.setEstdep(true);
    return ndepto;
}
}//fin de la clase Principal

和错误: as you can see, the table structure in the query does not correspond to the actual -you may refer to Usuarios.java

一切顺利

1 个答案:

答案 0 :(得分:1)

请查看&#39; Usuarios.java&#39;文件。它仍然有&#39; codsal&#39;:

do_something_unique if external_request?

这就是为什么hibernate仍在新数据库中查找该列的原因。