休眠中的TypeMisMatchException

时间:2018-09-11 21:27:00

标签: java hibernate

以下是我的Person类:

package com.subir.sample;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name ="person",uniqueConstraints = {@UniqueConstraint(columnNames= {"NAME"})})
public class Person implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = -2728179031744032393L;
int age;
String name;
char isVip;

public Person() {
}
public Person(int age, String name, char isVip) {
    this.age = age;
    this.name = name;
    this.isVip = isVip;
}
@Id
@Column(name="AGE")
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
@Id
@Column(name="NAME")
//@OneToMany(mappedBy="NAME")
private Set <Subject> subjects;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Id
@Column(name="ISVIP")
public char getIsVip() {
    return isVip;
}
public void setIsVip(char isVip) {
    this.isVip = isVip;
}
}

以下是我的用于添加和查看人员的课程。

package com.subir.sample;

import org.hibernate.Transaction;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class PersonDbAccess {
public static void addPerson(String name, int age, char isVip, Session session, org.hibernate.Transaction tx) {
    try {
        tx = session.beginTransaction();
        Person p = new Person(age, name, isVip);
        session.save(p);
        tx.commit();

    } catch (Exception e) {
        tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
}

public static void viewPerson(String name, Session session) {
    try {
        System.out.println("Name value in view method is :: " + name);
        Person person = (Person)session.get(Person.class, name);
        System.out.println("Person.class is ::" + Person.class + " Person.class.getName is :: "
                + Person.class.getName() + " Person.class.getSimpleName() is :: " + Person.class.getSimpleName()
                + " Person.class.getCanonicalName is :: " + Person.class.getCanonicalName());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }
}

public static void main(String[] args) {
    SessionFactory factory = HibernateUtils.buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = null;
    String name = "Subir";
    int age = 20;
    char isVip = 'Y';
    // addPerson(name,age,isVip,session,tx);
    viewPerson("Subir", session);
}
}

以下是我的堆栈跟踪:

Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.subir.sample.Person. Expected: class com.subir.sample.Person, got class java.lang.String
at org.hibernate.event.internal.DefaultLoadEventListener.checkIdClass(DefaultLoadEventListener.java:166)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1240)
at org.hibernate.internal.SessionImpl.access$1900(SessionImpl.java:204)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2842)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2816)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:1076)
at com.subir.sample.PersonDbAccess.main(PersonDbAccess.java:53)

即使我在object.class,String对象的get方法中传递了session,它也给我类型不匹配的异常。

1 个答案:

答案 0 :(得分:1)

您需要将ID传递给休眠会话的get方法。在您的情况下,您有一个复合键,仅按名称获取可能会导致返回多个对象,这与通过主键返回单个对象的get操作的性质相矛盾。

您需要在此处使用条件或查询。

另一方面,如果要使用Session.get方法,则应创建一个EmbededId并在Session.get方法中使用它,而不是用ID标记不符合JPA的多列。那么它将不会是不兼容的类型。

阅读https://vladmihalcea.com/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/

更新: 我可以看到您对名称具有唯一性约束。为什么需要AGE和isVip作为密钥的一部分,因为用ID标记它们实际上是使它们成为密钥的一部分。