gpt4 book ai didi

java - hibernate 中的 TypeMisMatchException

转载 作者:行者123 更新时间:2023-12-02 10:49:25 24 4
gpt4 key购买 nike

以下是我的 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;
}
}

以下是我用于添加和查看人员的类(class)。

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)

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

最佳答案

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

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

另一方面,如果您想使用 Session.get 方法,您应该创建一个 EmbededId 并在 Session.get 方法中使用它,而不是使用不兼容 JPA 的 ID 标记多个列。这样就不会出现不兼容类型了。

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

更新:我可以看到你对名称有唯一的限制。为什么您需要 AGE 和 isVip 作为 key 的一部分,因为用 ID 标记它们就可以有效地使它们成为 key 的一部分。

关于java - hibernate 中的 TypeMisMatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52284445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com