MySQL连接器无法使用复合主键创建表

时间:2018-07-09 15:37:57

标签: mysql hibernate jpa

我正在尝试创建带有Spring框架UserConnection使用的复合主键的实体。异常和类如下;

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank integer not null, refreshtoken varchar(512), secret varchar(512), primary k' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_161]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_161]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912) ~[mysql-connector-java-5.1.46.jar:5.1.46]

@Entity
@IdClass(UserConnectionId.class)
@Table(name = "Userconnection")
public class UserConnection implements Serializable {

@Id
@Column(name = "Userid", unique = true, length = 255, nullable = false)
private String userId;

@Id
@Column(name = "Providerid", length = 255, nullable = false)
private String providerId;

@Id
@Column(name = "Provideruserid", length = 255, nullable = false)
private String providerUserId;

@Column(name = "Rank", nullable = false)
private int rank;

@Column(name = "Displayname", length = 255, nullable = true)
private String displayName;

@Column(name = "Profileurl", length = 512, nullable = true)
private String profileUrl;

@Column(name = "Imageurl", length = 512, nullable = true)
private String imageUrl;

@Column(name = "Accesstoken", length = 512, nullable = true)
private String accessToken;

@Column(name = "Secret", length = 512, nullable = true)
private String secret;

@Column(name = "Refreshtoken", length = 512, nullable = true)
private String refreshToken;

@Column(name = "Expiretime", nullable = true)
private Long expireTime;
}

我有一个主键类;

public class UserConnectionId implements Serializable {

    private int userId;
    private int providerId;
    private int providerUserId;

    public UserConnectionId(int userId, int providerId, int providerUserId) {
        this.userId = userId;
        this.providerId = providerId;
        this.providerUserId = providerUserId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, providerId, providerUserId);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        UserConnectionId other = (UserConnectionId) obj;
        if (providerId != other.providerId)
            return false;
        if (providerUserId != other.providerUserId)
            return false;
        if (userId != other.userId)
            return false;
        return true;
    }
}

我不使用@IdClass而出现异常。作为社区推荐的@IdClass的解决方案用法。但是仍然有例外。你有没有例外?

谢谢。

1 个答案:

答案 0 :(得分:0)

最可能是因为Rank是MySQL保留的关键字(版本8 +):

https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-R

请更改列名以避免冲突或对其进行转义。对于后一种选择,这里有一些讨论:

Creating field with reserved word name with JPA