I have 2 tables:
UserDetails - holds all information to the user
UserDetailsHistory - tracks changes in the user status. The common column is userID.
One row in userDetails can have many rows in userDetailsHistory since the user can move between statuses.
What I want to display to the user: userID, firstName, lastName, userStatus, userCreationTs, userComments, lastUpdatedTs (the latest lastUpdatedTs in UserDetailsHistory).
Code given below. I get no results. What am I doing wrong?
@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);
Thanks in advance!
UPDATE: I tried the options mentioned in the comments, but could not get it to work.
The error I am getting is on the @ManytoOne association between the UserDetailsHistory and UserDetails table.
I think the tricky part is that UserDetails and UserDetailsHistory do not have a parent - child relationship. They are related through one common column userID.
So I need to find a way to combine these 2 tables using the common column.
Can anyone please help me out?
UPDATE 3: Error when I use the annotations @Id and @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]
...
...
...