使用JPA的Springboot。无法保存到SQL Server中的自动增量字段字段。空错误

时间:2017-11-08 21:53:13

标签: java sql-server api jpa spring-boot

enter image description here我试图通过Spring Boot with JPA学习一些API开发,我在数据库中有这个表,id列被指定为具有自动增量的主键。当我提交数据时,我不断收到此错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'Id', table

在我的课堂上我有这个id。你可能会说为什么@Column(name='Id')。我开始添加这个作为我的故障排除。在我从表中读取信息之后,在一些更改代码之后仍然可以正常工作。

package com.FUT.track.web.FUTtrackapplication.squads;

import javax.persistence.*;

@Entity
@Table(name="Squads")
public class Squad {

@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
@Column(name ="Id", nullable = false, unique = true)
public int id;
public String squadName;
public String squadDescription;
public String primaryFormation;


public Squad() {

}


public Squad(int id, String squadName, String squadDescription, String 

primaryFormation) {
    super();
    this.id = id;
    this.squadName = squadName;
    this.squadDescription = squadDescription;
    this.primaryFormation = primaryFormation;

}

public int getId() {
    return id;
}

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

public String getSquadName() {
    return squadName;
}

public void setSquadName(String squadName) {
    this.squadName = squadName;
}

public String getSquadDescription() {
    return squadDescription;
}

public void setSquadDescription(String squadDescription) {
    this.squadDescription = squadDescription;
}

public String getPrimaryFormation() {
    return primaryFormation;
}

public void setPrimaryFormation(String primaryFormation) {
    this.primaryFormation = primaryFormation;
}

}

不确定是否需要@GeneratedValue( strategy = GenerationType.IDENTITY)但是有或没有它我仍然会得到相同的错误。我提交除id以外的所有字段的数据。所有其他字段都可以存储空值,所以即使没有提交任何内容,我认为应该为新列生成id。但由于某种原因,它无法正常工作。

同样在我的应用程序属性文件中:

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

Try doing something like this:

import javax.persistence.*;

@Entity
@Table(name = "Squads")
public class Squad{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", nullable = false, unique = true)
    public int id;

    @Column(name = "SquadName", nullable = false)
    private String squadName;

    @Column(name = "SquadDescription", nullable = false)
    private String squadDescription;

    @Column(name = "PrimaryFormation", nullable = false)
    private String primaryFormation;

    public Squad(){

    }

    public Squad(final String squadName, final String squadDescription, final String primaryFormation){

        this.squadName = squadName;
        this.squadDescription = squadDescription;
        this.primaryFormation = primaryFormation;
    }

    public int getId(){

        return id;
    }

    public void setId(final int id){

        this.id = id;
    }

    public String getSquadName(){

        return squadName;
    }

    public void setSquadName(final String squadName){

        this.squadName = squadName;
    }

    public String getSquadDescription(){

        return squadDescription;
    }

    public void setSquadDescription(final String squadDescription){

        this.squadDescription = squadDescription;
    }

    public String getPrimaryFormation(){

        return primaryFormation;
    }

    public void setPrimaryFormation(final String primaryFormation){

        this.primaryFormation = primaryFormation;
    }
}

Notice how I do not include the "id" in the constructor. Also, I've added column names for each field. I assumed that you or your DBA will want to use camel case names instead of undersored names, so in your "application.properties" file (or yml ... whatever), set the physical naming strategy like so:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Most database developers and administrators that work with Microsoft SQL Server prefer camel case for table and column names, as opposed to using the underscores approach that JPA and Spring assume to be the default. For example, most of the time in Java/Spring you expect a table name of "MY_TABLE", but in SQL Server you will probably have a table name of "MyTable". Since this is non-standard in the spring ecosystem, you need to specify a different naming strategy.