Home How do I use the jpa/hibernate annotation mapping to Set<Entity> and the Entity is composite primary key
Reply: 2

How do I use the jpa/hibernate annotation mapping to Set<Entity> and the Entity is composite primary key

TJ Season
1#
TJ Season Published in 2018-01-11 06:11:12Z
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "users")
@ToString
@Table(name = "users")
@Embeddable
public class User implements UserDetails{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id",unique=true)
    private Long id;
    @OneToMany
    @Cascade({CascadeType.ALL })
    @JoinTable(name = "users_authorities")
    private Set<SimpleGrantedAuthority> authorities;
}

This is my User.java,and have a porperty private Set<SimpleGrantedAuthority> authorities. I want use jpa/hibernate mapping to another Entity SimpleGrantedAuthority.

@Data
@Entity(name = "authorities")
@Embeddable
@Table(name = "authorities", uniqueConstraints={@UniqueConstraint(columnNames={"authority","username"})})
class SimpleGrantedAuthority implements GrantedAuthority {
    private Long id;
    @Column(name = "authority")
    private String authority;
    @Column(name = "username")
    private String username;
}

This is SimpleGrantedAuthority .java,How do I write the correct annotation to map Set<SimpleGrantedAuthority> authorities; to SimpleGrantedAuthority.javaabove private Set<SimpleGrantedAuthority> authorities;

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 org.hibernate.AnnotationException: No identifier specified for entity: com.as.backend.antscience.entity.SimpleGrantedAuthority
Jobin
2#
Jobin Reply to 2018-01-11 06:52:51Z

@JoinColumns annotation is probably what you are looking for.

https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/JoinColumns.html

Javvano
3#
Javvano Reply to 2018-01-11 06:26:47Z

if you need an express composite key in JPA , you have to write as below. first , define composite key as class and put a @Embeddable annotation. and define that Id class as member of entitiy class and put @EmbeddedId

@Data
@Entity(name = "authorities")
@Embeddable
@Table(name = "authorities", uniqueConstraints={@UniqueConstraint(columnNames={"authority","username"})})
class SimpleGrantedAuthority implements GrantedAuthority {

    @EmbeddedId
    private Id id;

    @Embeddable
    @EqualsAndHashCode
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class Id implements Serializable {

        private static final long serialVersionUID = 1L;

        @Column(name = "authority")
        private String authority;

        @Column(name = "username")
        private String username;
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322182 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO