繁体   English   中英

JPA复合外键

[英]JPA Composite Foreign Primary keys

因此,我目前有一个包含用户信息的数据库,定义用户的是user_id。

然后,我有了名为token的表,该表具有token_id和user_id作为主键以及其余信息,从而使它成为一对多的数据库。

@Entity
@Table(name = "user")
public class User implements Serializable {
    @Id
    @Column(name = "user_id")
    private long userId;
    //Other variables and getters and setters

    @OneToMany(orphanRemoval = true, mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @Access(AccessType.PROPERTY) //I need this as is since I have other things in the setter 
    private List<Token> tokens = new ArrayList<>();

    public List<Token> getTokens() {
        return tokens;
    }

    public void setTokens(List<Token> tokens) {
        this.tokens = tokens;
    }
}

在这段代码片段之后,我有了令牌的类

public class Token implements Serializable{
    @Id
    private long tokenId;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User user;

    @Column(nullable = false)
    private String token;

    @Access(AccessType.PROPERTY)
    private Instant lastUsed;

    @Column(nullable = false)
    private Instant dateCreated;

    @Transient
    private boolean expired;

    //Getters and setters go here


    //Static methods and generating the token
    private static String generateToken(){
        Random random = new Random();
        byte[] randomString = new byte[256];
        random.nextBytes(randomString);
        return Base64.encodeBase64String(randomString);
    }

    public static Token generateUserToken(User user){
        Token token = new Token();
        token.setTokenId(new Random().nextLong());
        token.setUser(user);
        token.setDateCreated(Instant.now());
        token.setToken(generateToken());

        return token;
    }
    //Static methods and generating the token
}

现在由于某种原因,只要没有将User user标记为@Id,它就可以工作(即使在数据库中它是主键)。

任何帮助;

application.properties:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57InnoDBDialect

spring.jpa.show-sql=true
logging.level.org.hibernate.type=TRACE

SQL输出:

Hibernate: insert into tokens (date_created, last_used, token, user_id, token_id) values (?, ?, ?, ?, ?)
binding parameter [1] as [TIMESTAMP] - [2018-05-14T08:29:00.719764Z]
binding parameter [2] as [TIMESTAMP] - [null] //This is okay to be null this is last_used
binding parameter [3] as [VARCHAR] - [<Token too long to write in question>] //Actual data type is LONGTEXT
binding parameter [4] as [BIGINT] - [null] //this is a problem (user_id should not be - should be a long numebr such as: 5531405900210671089)
binding parameter [5] as [BIGINT] - [0] //this is a problem (token_id should be a long number such as: -8824825685434914749)
SQL Error: 1048, SQLState: 23000
Column 'user_id' cannot be null

您无需在Token中用@Id注释user_id:您看到了这可行。 同样在数据库中,足以将表令牌的主键定义为tokednId。 当然,必须将user_id设置为外键,不能为空。

这是一个“派生身份”,因此Token需要这样的@IdClass

public class TokenId implements Serializable {
    private long tokenId; // matches the name of the attribute
    private long user;  // matches name of attribute and type of User PK
    ...
}

然后, Token需要这样指定其@IdClass

@Entity
@IdClass(TokenId.class)
public class Token {
    ...
}

JPA 2.1规范的第2.4.1节中讨论了派生身份(带有示例)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM