I have two classes, a User and a LocalUser. LocalUser uses a FK to User as it's PK. When I try to create a LocalUserRepository, I receive the exception: java.lang.IllegalArgumentException: This class [~.LocalUser] does not define an IdClass.
I have been searching for an answer for a few hours and everything I have tried has not worked.
@Entity( name = "User" )
@Table( name = "users" )
public class User implements Serializable
{
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY, generator = "user_id_seq" )
@Column( name = "id", updatable = false, columnDefinition = "BIGSERIAL" )
private Long id;
@Column( name = "given_name", nullable = false )
private String givenName;
@Column( name = "family_name" )
private String familyName;
@Enumerated( EnumType.STRING )
@Column( name = "role", nullable = false, columnDefinition = "VARCHAR(32)" )
private Role role;
...
}
@Entity( name = "LocalUser" )
@Table( name = "local_user" )
@IdClass( User.class )
public class LocalUser implements Serializable
{
@Id
@OneToOne( optional = false, orphanRemoval = true )
@JoinColumn( name = "user_id", nullable = false, unique = true )
private User user;
@Column( name = "username", unique = true, nullable = false )
private String username;
@Column( name = "password", nullable = false )
private String password;
...
}
public interface LocalUserRepo extends JpaRepository<LocalUser, User>
{
@Query( "SELECT u.user FROM LocalUser u WHERE u.username = :username AND u.password = :password" )
User getUser( String username, String password );
}
The desired result is two tables with matching this schema:
user
id [PK] | given_name | family_name | role |
---|---|---|---|
1 | Example | User | User |
local_user
user_id [PK,FK(user)] | username | password |
---|---|---|
1 | exampleuser | ######## |
After much searching, I finally managed to accomplish what I was attempting. I needed to add an id to LocalUser and map it to a user. Here is what I did:
@Entity
@Table( name = "local_users" )
public class LocalUser
{
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
@Column( name = "user_id", nullable = false )
private Long id;
@MapsId
@OneToOne( fetch = FetchType.EAGER, optional = false )
@JoinColumn( name = "user_id", nullable = false, foreignKey = @ForeignKey( name = "local_users_user_id_fk" ) )
private User user;
@Column( name = "password", nullable = false )
private String password;
@Column( name = "username", nullable = false, unique = true )
private String username;
}
@Entity
@Table( name = "users" )
public class User
{
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY, generator = "user_id_seq" )
@Column( name = "id", updatable = false, columnDefinition = "BIGSERIAL" )
private Long id;
@Column( name = "given_name", nullable = false )
private String givenName;
@Column( name = "family_name" )
private String familyName;
@Enumerated( EnumType.STRING )
@Column( name = "role", nullable = false, columnDefinition = "VARCHAR(32)" )
private Role role;
...
}
@Repository
public interface LocalUserRepo extends JpaRepository<LocalUser, Long>
{
@Query( "SELECT u.user FROM LocalUser u WHERE u.username = :username AND u.password = :password" )
User getUser( String username, String password );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.