简体   繁体   English

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

[英]Map a String as Primary Key in Hibernate

I'm developing a project where I have a class called Conductor (Driver, in spanish). 我正在开发一个项目,我有一个名为Conductor(Driver,西班牙语)的课程。 This Conductor class has a parameter LatLng (containing a position latitude-longitude). 此Conductor类具有参数LatLng(包含位置纬度 - 经度)。 I want these two classes to persist them in my database using Hibernate, with which I have some experience. 我希望这两个类使用Hibernate将它们保存在我的数据库中,我有一些经验。

Here you are the classes with their hiberate's mapping files: 这里是你的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>

The problem arises when, in the Dao class, I perform this operation: 当在Dao类中执行此操作时出现问题:

public Conductor anadirConductor(String pEmail, String pNombre, String pTfno, String pDomicilio, double pLat, double pLon) throws ExisteConductorException { System.out.println("Creando nuevo conductor"); 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();
    }
}

... using this line Conductor c = new PtDaoService().anadirConductor(email, nombre, tfno, domicilio, lat, lon); ...使用此行导体c = new PtDaoService()。anadirConductor(email,nombre,tfno,domicilio,lat,lon); an Exception is caught throwing the following message: 捕获异常会抛出以下消息:

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

GRAVE: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.ingartek.cavwebapp.model.Conductor at org.hibernate.id.Assigned.generate(Assigned.java:52).... GRAVE:org.hibernate.id.IdentifierGenerationException:在调用save()之前必须手动分配此类的ID:org.hibernate.id.Assigned.generate中的com.ingartek.cavwebapp.model.Conductor(Assigned.java:52) ....

Which is my error? 这是我的错误?

Need help :( 需要帮忙 :(

Thank you 谢谢

Solved. 解决了。 The constructor I was using in my Dao was not assigning anything... 我在Dao中使用的构造函数没有分配任何东西......

It should have been that way: 应该是这样的:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM