gpt4 book ai didi

java - Hibernate - 这个映射是否正确?无法通过反射 getter 获取字段值?

转载 作者:行者123 更新时间:2023-11-28 22:41:30 25 4
gpt4 key购买 nike

为什么会出现此错误?你能帮帮我吗?我使用 Hibernate 3.5.4-Final、MySQL 5.6、Tomcat 8。

Exception in thread "main" org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.hibernatetest.entity.User.userId
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:62)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:230)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3852)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3560)`enter code here`
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:204)
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:449)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:141)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:567)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1612)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2294)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
at org.hibernate.loader.Loader.list(Loader.java:2167)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.hibernatetest.repository.MessageRepository.outbox(MessageRepository.java:81)
at com.hibernatetest.test.Test.main(Test.java:34)

Caused by: java.lang.IllegalArgumentException: Can not set long field com.hibernatetest.entity.User.userId to java.lang.Long
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
at sun.reflect.UnsafeLongFieldAccessorImpl.getLong(Unknown Source)
at sun.reflect.UnsafeLongFieldAccessorImpl.get(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:59)
... 22 more

这是我的用户实体:

@Entity
@Table(name = "User")
@XmlRootElement
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userId", updatable = false, nullable = false)
private long userId;

@Column(name = "userName", nullable = false, unique = true, length = 89)
private String userName;

@Column(name = "userPassword", nullable = false, length = 34)
private String userPassword;

@JsonIgnore
@OneToMany(mappedBy = "senderUserId")
private List<Message> outbox;

@JsonIgnore
@OneToMany(mappedBy = "receiverUserId")
private List<Message> inbox;

...GETTER and SETTER...

}

这是我的消息实体:

@Entity
@Table(name = "Message")
@XmlRootElement
public class Message {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "messageId")
private long messageId;

@Column(name = "messageText")
private String messageText;

@ManyToOne
@JoinColumn(name = "senderUserId")
private User senderUserId;

@ManyToOne
@JoinColumn(name = "receiverUserId")
private User receiverUserId;

...GETTER and SETTER...

}

这是我的 MessageRepository 发件箱方法:

@SuppressWarnings("unchecked")
public List<Message> outbox(long senderUserId) {
session.getTransaction().begin();
Query query = session.createQuery("from Message where senderUserId = :senderUserId");
query.setParameter("senderUserId", senderUserId);
session.beginTransaction().commit();
return query.list();
}

这是我的基本测试类。当我运行这个主要方法时。顶部出现错误。

public class Test {

public static void main(String[] args) {


MessageRepository messageRepository = new MessageRepository();
UserRepository userRepository = new UserRepository();

User user = userRepository.get(1);

List<Message> messages = messageRepository.outbox(user.getUserId());
for (Message message : messages) {
System.out.println(message.getMessageText());
}
}

最佳答案

查询应该是:

Query query = session.createQuery("select m from Message m where m.senderUserId.userId = :senderUserId");

此外,您真的设计了所有的 getter 和 setter 方法吗?

关于java - Hibernate - 这个映射是否正确?无法通过反射 getter 获取字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32539098/

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