0

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]

    ...

    ...

    ...
Deepboy
  • 523
  • 3
  • 16
  • 28
  • The typo in `findFirstByUserIDOrderBylastUpdatedTsDesc` is probably the cause of the problem, it should be `findFirstByUserIDOrderByLastUpdatedTsDesc`. The L is not uppercase in your method. – YoannFleuryDev Jul 24 '18 at 21:05
  • What repository is this in? Why are you returning a `List` and not a `List – Jens Schauder Jul 25 '18 at 06:01
  • Thankyou, let me try with the updated method name. I will also change List to List – Deepboy Jul 25 '18 at 13:10
  • What is the error you are getting ? – PaulNUK Jul 25 '18 at 13:55
  • @PaulNUK, question updated with error I am getting. – Deepboy Jul 25 '18 at 16:41
  • This looks like a bug in hibernate but other's have encountered this. See here for example: https://stackoverflow.com/questions/35314780/jpa-hibernate-null-pointer-exception-with-onetoone-primary-key-foreign-key-type – PaulNUK Jul 31 '18 at 13:37

1 Answers1

0

I'm assuming you are using Hibernate with Spring Data JPA. If so while saving new entity of class UserDetailsHistory Hibernate may overwrite whatever was in field userId with a generated ID. Try (you must add the necessary column to the database table as well):

@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);
Konrad Botor
  • 4,765
  • 1
  • 16
  • 26