在Hibernate中将字符串映射为主键

时间:2015-02-17 12:07:43

标签: java hibernate vaadin vaadin7

我正在开发一个项目,我有一个名为Conductor(Driver,西班牙语)的课程。此Conductor类具有参数LatLng(包含位置纬度 - 经度)。我希望这两个类使用Hibernate将它们保存在我的数据库中,我有一些经验。

这里是你的hiberate映射文件的类:

public class LatLng implements Serializable {


/*
 * Atributos
 */
private static final long serialVersionUID = -234127535402360915L;

private long id;
private double latitud;
private double longitud;

/*
 * Métodos
 */
public LatLng(){

}

public LatLng(double latitud, double longitud){
    this.latitud = latitud;
    this.longitud = longitud;
}

public long getId() {
    return id;
}

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

public double getLatitud() {
    return this.latitud;
}

public void setLatitud(double value) {
    this.latitud = value;
}

public double getLongitud() {
    return this.longitud;
}

public void setLongitud(double value) {
    this.longitud = value;
}

@Override
public String toString() {
    return "Latitud: " + this.latitud + ", longitud: " + this.longitud;
}

@Override
public int hashCode() {
    int result;
    result = (int) (id + latitud + longitud);
    return result;
}

@Override
public boolean equals(Object other) {
    if(other==null)
        return false;

    if( !(other instanceof Conductor) )
        return false;

    LatLng pos = (LatLng) other;

    return pos.getId()==this.id && pos.getLatitud()==this.latitud && pos.getLongitud()==this.longitud;

}

}

<!DOCTYPE 
hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN" 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<class name="com.ingartek.cavwebapp.model.LatLng" table="LATLNG">

    <id name="id" type="long" column="LATLNG_ID">
        <generator class="native"/>
    </id>

    <property name="latitud" type="double" column="LATLNG_LATITUD" not-null="true" />
    <property name="longitud" type="double" column="LATLNG_LONGITUD" not-null="true" />

</class>

public class Conductor implements Serializable {

/*
 * Atributos
 */
private static final long serialVersionUID = 4492787316635472774L;

private String email;
private String nombre;
private String telefono;
private String domicilio;
private LatLng domicilioCoord;

/*
 * Métodos
 */
public Conductor(){

}

public Conductor(String email, String nombre, String telefono, String domicilio,
        LatLng domicilioCoord){

}

public String getNombre() {
    return this.nombre;
}

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

public String getEmail() {
    return this.email;
}

public void setEmail(String value) {
    this.email = value;
}

public String getTelefono() {
    return this.telefono;
}

public void setTelefono(String value) {
    this.telefono = value;
}

public String getDomicilio() {
    return this.domicilio;
}

public void setDomicilio(String value) {
    this.domicilio = value;
}

public LatLng getDomicilioCoord() {
    return this.domicilioCoord;
}

public void setDomicilioCoord(LatLng value) {
    this.domicilioCoord = value;
}

@Override
public int hashCode() {
    return email.hashCode() + nombre.hashCode() + telefono.hashCode() + domicilio.hashCode() + domicilioCoord.hashCode();
}

@Override
public boolean equals(Object other) {
    if(other==null)
        return false;

    if( !(other instanceof Conductor) )
        return false;

    Conductor c = (Conductor) other;

    return c.getEmail().equals(this.email) 
            && c.getNombre().equals(this.nombre)
            && c.getTelefono().equals(this.telefono)
            && c.getDomicilio().equals(this.domicilio)
            && c.getDomicilioCoord().equals(this.domicilioCoord);
}

}

<!DOCTYPE hibernate-mapping PUBLIC 
"Hibernate Mapping 3.0" 
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd" >

<class name="com.ingartek.cavwebapp.model.Conductor" table="CONDUCTORES">

    <!-- Las clases referenciadas no se habían creado -->

    <id name="email" type="text" column="CONDUCTORES_EMAIL">
        <generator class="assigned" />
    </id>

    <property name="nombre" type="string" column="CONDUCTORES_NOMBRE" not-null="true" />
    <property name="telefono" type="string" column="CONDUCTORES_TELEFONO" not-null="true" />
    <property name="domicilio" type="string" column="CONDUCTORES_DOMICILIO" not-null="true" />

    <!-- Clases referenciadas -->
    <many-to-one name="domicilioCoord" column="CONDUCTORES_DOCIMILIOCOORD" class="com.ingartek.cavwebapp.model.LatLng" cascade="remove" />

</class>

在Dao课程中,我执行此操作时会出现问题:

public Conductor anadirConductor(String pEmail,String pNombre,String pTfno,              String pDomicilio,double pLat,double pLon)抛出ExisteConductorException {         System.out.println(“Creando nuevo conductor”);

    if(!isEmailUsado(pEmail)){
        beginTransaction();
        System.out.println("Creando el conductor");
        LatLng pos = new LatLng(pLat, pLon);
        System.out.println("El identificador de la posicion es " + pos.getId());
        Conductor unConductor = new Conductor(pEmail, pNombre, pTfno, pDomicilio, pos);

        if(!flota.existeConductor(unConductor)){
            flota.anadirConductor(unConductor);
        }

        session.save(pos);
        session.save(unConductor);
        commitTransaction();
        return unConductor;     
    }else{
        rollbackTransaction();
        throw new ExisteConductorException();
    }
}

...使用此行导体c = new PtDaoService()。anadirConductor(email,nombre,tfno,domicilio,lat,lon);发现异常会抛出以下消息:

feb 17, 2015 1:05:40 PM com.vaadin.server.DefaultErrorHandler doDefault

GRAVE:org.hibernate.id.IdentifierGenerationException:在调用save()之前必须手动分配此类的ID:com.ingartek.cavwebapp.model.Conductor     在org.hibernate.id.Assigned.generate(Assigned.java:52)....

我的错误是什么?

需要帮助:(

谢谢

1 个答案:

答案 0 :(得分:0)

解决。我在Dao中使用的构造函数没有分配任何东西......

应该是这样的:

public Conductor(String email, String nombre, String telefono, String domicilio,
        LatLng domicilioCoord){
    this.email = email;
    this.nombre = nombre;
    this.telefono = telefono;
    this.domicilio = domicilio;
    this.domicilioCoord = domicilioCoord;
}
相关问题