JPA正在为我的INT字段创建一个VARCHAR列

时间:2016-03-02 14:28:46

标签: java mysql jpa

大家,

目前我正在尝试根据我的班级规范创建一个表格,并且我收到的错误是"列错误的列说明符' ID'&#34 ;。这是因为我的ID字段是VARCHAR(255)并且具有AUTO_INCREMENT,正如您在以下错误中看到的那样:

Error executing: CREATE TABLE gameuserstate (ID VARCHAR(255) AUTO_INCREMENT NOT NULL, ADDRESS VARCHAR(255), CITY VARCHAR(255), DOCUMENTID VARCHAR(255), FBUSERID VARCHAR(255), IDENTIFIER VARCHAR(255), LASTNAME VARCHAR(255), NAME VARCHAR(255), PASSWORD VARCHAR(255), STATE VARCHAR(255), USERSTATE LONGTEXT, USERID VARCHAR(255), ZIPCODE VARCHAR(255), PRIMARY KEY (ID))

问题是:我的班级' ID字段类型是INT! JPA如何使用VARCHAR(255)字段来表示int?

这是我的实体类:

@Table(name = "gameuserstate")
@Cacheable(false)
@Entity
public class JpaGameUserState {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private int id;

    @Column
    private String userid;

    @PrimaryKeyJoinColumn
    private JpaAccount account;

    @Column
    private String fbUserId;

    @Column
    private String identifier;

    @Column
    private String password;

    @Column
    private String name;

    @Column
    private String lastName;

    @Column
    private String documentId;

    @Column
    private String state;

    @Column
    private String city;

    @Column
    private String address;

    @Column
    private String zipcode;

    // this will hold old userstate object format
    @Lob
    @Column
    private String userState;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public JpaAccount getAccount() {
        return account;
    }

    public void setAccount(JpaAccount account) {
        this.account = account;
    }

    public String getFbUserId() {
        return fbUserId;
    }

    public void setFbUserId(String fbUserId) {
        this.fbUserId = fbUserId;
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getDocumentId() {
        return documentId;
    }

    public void setDocumentId(String documentId) {
        this.documentId = documentId;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public String getUserState() {
        return userState;
    }

    public void setUserState(String userState) {
        this.userState = userState;
    }

}

3 个答案:

答案 0 :(得分:0)

您是否阅读了[所有类型的@Generated] [1]

[1] http://www.objectdb.com/java/jpa/entity/generated

答案 1 :(得分:0)

如果您没有向实体提供序列定义,JPA将不会应用auto_increment,但它将应用序列为新插入的行分配新ID,因此您应该使用GenerationType.IDENTITY 像这样

 @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

答案 2 :(得分:0)

今天我已经找到了答案。

问题在于我在帐户属性中使用了@PrimaryKeyJoinColumn注释。删除它使一切都按预期工作。