我有2张桌子:
UserDetails-将所有信息保留给用户
UserDetailsHistory-跟踪用户状态的更改。 公用列是userID。
userDetails中的一行可以在userDetailsHistory中包含多行,因为用户可以在状态之间移动。
我要向用户显示的内容: userID,firstName,lastName,userStatus,userCreationTs,userComments,lastUpdatedTs(UserDetailsHistory中的最新lastUpdatedTs)。
下面给出的代码。我没有结果。 我在做什么错了?
@Entity
public class UserDetails {
@Id
private String userID;
private String firstName;
private String lastName;
private String userStatus;
private Timestamp userCreationTs;
private List<String> userComments;
}
@Entity
public class UserDetailsHistory {
@Id
@ManyToOne
@JoinColumn(name="userID")
private String userID;
private boolean userStatusChange;
private String userStatusOld;
private String userStatusNew;
private TimeStamp lastUpdatedTs;
}
In repository:
public List<Object> findFirstByUserIDOrderBylastUpdatedTsDesc(userID);
谢谢!
UPDATE : 我尝试了评论中提到的选项,但无法正常工作。
我遇到的错误是UserDetailsHistory和UserDetails表之间的@ManytoOne关联。
我认为棘手的部分是 UserDetails和UserDetailsHistory没有父子关系。它们通过一个公用列userID 关联。
因此,我需要找到一种使用公共列组合这两个表的方法。
有人可以帮我吗?
更新3: 使用注释@Id和@JoinColumn时出错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at
...
...
Caused by: java.lang.NullPointerException: null
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1708) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1617) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
...
...
...
答案 0 :(得分:0)
我假设您正在使用带有Spring Data JPA的Hibernate。如果是这样,则在保存类UserDetailsHistory的新实体时,Hibernate可能会覆盖具有已生成ID的userId字段中的所有内容。试试(您还必须将必要的列也添加到数据库表中):
@Entity
public class UserDetails {
@Id
private String userID;
private String firstName;
private String lastName;
private String userStatus;
private Timestamp userCreationTs;
private List<String> userComments;
}
@Entity
public class UserDetailsHistory {
@Id
private String ID;
@ManyToOne
@JoinColumn(name="userID")
private String userID;
private boolean userStatusChange;
private String userStatusOld;
private String userStatusNew;
private TimeStamp lastUpdatedTs;
}
In repository:
public List<UserDetailsHistory> findFirstByUserIDOrderBylastUpdatedTsDesc(userID);