如何修复列"id"中的“空值"违反非空约束'' [英] How to fix ''null value in column "id" violates not-null constraint''

查看:1065
本文介绍了如何修复列"id"中的“空值"违反非空约束''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了spring mvc项目,我想使用Hibernate(v5)将模型类添加到DB(postgres v10)中,但是当执行程序时,显示错误"id"列中的空值违反了-null约束"

I'm created spring mvc project ,and i want to added model class in to DB(postgres v10) with Hibernate(v5) but when execute program, show error "null value in column "id" violates not-null constraint"

我在下面显示代码的重要部分

I show the important parts of the code in below

@Entity
@Table(name = "trs")
public class infoExcel {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private Integer rule_id;
    private String device_id;
    private String ts_date;
    private String ts_time;
    private String op_type;
    private String detail_pay;
    private String card_num;
    private String ref_num;
    private String tr_code;
    private String price;
    private String month_code;
    private Long tr_comm;


    public infoExcel() {
    }

    public infoExcel(String device_id, String ts_date, String ts_time, String op_type, String detail_pay, String card_num, String ref_num, String tr_code, String price, String month_code, Long tr_comm) {
        this.device_id = device_id;
        this.ts_date = ts_date;
        this.ts_time = ts_time;
        this.op_type = op_type;
        this.detail_pay = detail_pay;
        this.card_num = card_num;
        this.ref_num = ref_num;
        this.tr_code = tr_code;
        this.price = price;
        this.month_code = month_code;
        this.tr_comm = tr_comm;
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getRule_id() {
        return rule_id;
    }

    public void setRule_id(Integer rule_id) {
        this.rule_id = rule_id;
    }

    public String getDevice_id() {
        return device_id;
    }

    public void setDevice_id(String device_id) {
        this.device_id = device_id;
    }

    public String getTs_date() {
        return ts_date;
    }

    public void setTs_date(String ts_date) {
        this.ts_date = ts_date;
    }

    public String getTs_time() {
        return ts_time;
    }

    public void setTs_time(String ts_time) {
        this.ts_time = ts_time;
    }

    public String getOpr_type() {
        return op_type;
    }

    public void setOpr_type(String opr_type) {
        this.op_type = opr_type;
    }

    public String getDetail_pay() {
        return detail_pay;
    }

    public void setDetail_pay(String detail_pay) {
        this.detail_pay = detail_pay;
    }

    public String getCard_number() {
        return card_num;
    }

    public void setCard_number(String card_number) {
        this.card_num = card_number;
    }

    public String getReference_number() {
        return ref_num;
    }

    public void setReference_number(String reference_number) {
        this.ref_num = reference_number;
    }

    public String getTs_code() {
        return tr_code;
    }

    public void setTs_code(String ts_code) {
        this.tr_code = ts_code;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getMonth_code() {
        return month_code;
    }

    public void setMonth_code(String month_code) {
        this.month_code = month_code;
    }

    public Long getTs_com() {
        return tr_comm;
    }

    public void setTs_com(Long ts_com) {
        this.tr_comm = ts_com;
    }

    @Override
    public String toString() {
        return "infoExcel{" +
                "device_id='" + device_id + '\'' +
                ", ts_date='" + ts_date + '\'' +
                ", ts_time='" + ts_time + '\'' +
                ", opr_type='" + op_type + '\'' +
                ", card_number='" + card_num + '\'' +
                ", reference_number='" + ref_num + '\'' +
                ", ts_code='" + tr_code + '\'' +
                ", price='" + price + '\'' +
                ", month_code=" + month_code +
                ", ts_com=" + tr_comm +
                '}';
    }

以及如下所示的dispatcher-servlet.xml和application-context.xml

and dispatcher-servlet.xml and application-context.xml shown below

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <context:component-scan base-package="com.dsr"/>

    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

    <!-- File Upload Configuration Bean Details -->
    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000000000"/>
    </bean>

    <tx:annotation-driven/>
</beans>

和应用程序上下文是

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">



    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/dsr"/>
        <property name="username" value="postgres"/>
        <property name="password" value="123"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.dsr</value>
            </list>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10240000"/>
    </bean>
</beans>

我的存储库是


@Repository
@Transactional
public class FileUploadDaoImpl implements FileUploadDao {

    @Autowired
    private SessionFactory sessionFactory;
    Session session;

    public void addExcel(infoExcel infoExcel) {

        try {
             session = sessionFactory.getCurrentSession();
        }
        catch (HibernateException e)
        {
            session = sessionFactory.openSession();
        }

        session.saveOrUpdate(infoExcel);
        session.flush();
    }
}

推荐答案

我将其用于在PostgreSQL中创建序列生成器.我正在使用Long,但是我认为使用Integer时,此代码也可以工作.

I use this for create sequence generator in PostgreSQL. I`m using Long, but I think with Integer this code work too.

@Id
@GeneratedValue(
   strategy = GenerationType.SEQUENCE,
   generator = "entity_id_seq"
)
@SequenceGenerator(
   name = "entity_id_seq",
   sequenceName = "global_id_sequence",
   allocationSize = 1
) 
@Column(
   name = "id",
   unique = true,
   updatable = false,
   nullable = false
)
protected Long id;

这篇关于如何修复列"id"中的“空值"违反非空约束''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆