Reputation: 2055
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!
Upvotes: 0
Views: 1267
Reputation: 8246
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.
Upvotes: 1