0

I am trying to build a simple SpringBoot and Hibernate app using DAO and DTO pattern.

I am trying to save a list of users to the database.

When I am using User class it works fine, but when I am trying to use DTO CreateUserDto class I am getting the following error:

"Unknown entity: com.app.sportapp.dto.CreateUserDto; nested exception is org.hibernate.MappingException: Unknown entity: com.app.sportapp.dto.CreateUserDto"

There is a SingleTable inheritance where Player class and Coach class inherit User class.

User.java

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Getter
@Setter
@Entity(name = "Users")
@ApiModel(description = "All details about user")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "User_Type", discriminatorType= DiscriminatorType.STRING)
public class User implements Seriaalizable{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;   
    private String lastName;    
    private String username;    
    private String email;    
    private String password;   
    private String contactNumber;
}

Player.java

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Getter
@Setter
@Entity(name = "Players")
@DiscriminatorValue(value = "player")
@DiscriminatorOptions(force=true)
public class Player extends User {...}

Coach.java

@Entity(name = "Coaches")
@DiscriminatorValue(value = "coach")
@DiscriminatorOptions(force=true)
public class Coach extends User{

}

And here are DTO's:

CreateUserDto.java

public class CreateUserDto {...}

PlayerDto.java

public class PlayerDto extends CreateUserDto{...}

CoachDto.java

public class CoachDto extends CreateUserDto{


}

As I am very new to DAO and DTO pattern from error I am getting I assume that it is expected to have a model with @Entity called CreateUser so same name as DTO CreateUserDto? Or can I have the example what I did to have a User model and create a new CreateUserDto?

Thanks!

user9347049
  • 1,927
  • 3
  • 27
  • 66
  • If you are already defining some entities, why do you need the dtos? – Davide D'Alto Apr 12 '21 at 09:58
  • You think I should use it just for displaying user information and not for creation of user? – user9347049 Apr 12 '21 at 10:03
  • DTOs should be used for passing the data from FE to BE and other way around. But you also need converters to convert from one to other. When saving you save entity and not a DTO. e.g. FE sends a DTO, you copy properties to entity and use the repo to save entity – mirzak Apr 12 '21 at 10:05
  • @user9347049 This is my personal opinion and it really depends from your app. But if your DTOs look exactly like your entities, I don't see how having multiple copies of the same classes helps. As far as I can tell, a detached entity behaves the same as a DTO. But it really depends how your application is organised and I suppose others might have different opinions about this. – Davide D'Alto Apr 12 '21 at 10:11
  • @Davide thanks for your answer. So probably when creating new User in the database I should use entities but when retrieving them and displaying DTO's just to send data that is needed. – user9347049 Apr 12 '21 at 10:37
  • You definitely need entities to create a new user in the database with JPA :-) – Davide D'Alto Apr 12 '21 at 10:42

1 Answers1

1

The error happens because you are treating a DTO as an entity. Remove the JPA annotations from the DTOs and don't use those classes for connecting to the db.

You will convert the results from your queries from entities to DTO and vice-versa.

I would also suggest to have a look at Mapstruct for the creation of DTO. This will probably make it easier to separate the entities from the DTOs.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • Hi I edited my code. You can take a look at the update in my question. So I removed annotations from my DTO's and added converter using `model mapper`, now I am getting status `200 OK` but when I check database there is no newly created user. – user9347049 Apr 12 '21 at 10:35
  • It's probably because you are not flushing the session or committing the transaction at the end of the method. Maybe you can add `@Transactional` to the method for example. – Davide D'Alto Apr 12 '21 at 10:38
  • I added new changes added commit transaction to my method and `@Transactional` to my service but still not getting any data in my database. Do you know why? – user9347049 Apr 12 '21 at 11:15
  • I don't mind to help, but you are not supposed to constantly change your question on StackOverflow. My answer was for your original question and it seems it was correct. I think a better place to ask for help in this case is the Spring data room on Gitter: https://gitter.im/spring-projects/spring-data?source=orgpage – Davide D'Alto Apr 12 '21 at 11:21
  • By the way, the commit should occur after the loop. Not sure what else could be wrong. – Davide D'Alto Apr 12 '21 at 11:24