字段customer_id没有默认值

时间:2017-12-25 17:19:38

标签: hibernate

我在执行代码时遇到错误。你能告诉我我正在做的错误吗?

以下是错误消息。

Hibernate: insert into INSUBUY_CUSTOMER (CUSTOMER_DOB, CUSTOMER_NAME) values (?, ?)
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
    at com.insubuy.controller.App.main(App.java:51)
Caused by: java.sql.SQLException: Field 'CUSTOMER_ID' doesn't have a default value
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)

以下是我正在执行的代码。

Policy.java

package com.insubuy.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name="INSUBUY_CUSTOMER_POLICY_DETAILS")
public class Policy implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="POLICY_ID")
    int policy_id;

    @Column(name="POLICY_START_DATE")
    String policyStartDate;

    @Column(name="POLICY_END_DATE")
    String policyEndDate;

    @GenericGenerator(name = "generator", strategy = "foreign",
    parameters = @Parameter(name = "property", value = "INSUBUY_CUSTOMER"))
    @GeneratedValue(generator = "generator")
    @Column(name="CUSTOMER_ID")
    int customer_id;

    public int getCustomer_id() {
        return customer_id;
    }

    public void setCustomer_id(int customer_id) {
        this.customer_id = customer_id;
    }

    public Policy() {
        super();
    }

    public int getPolicy_id() {
        return policy_id;
    }



    public void setPolicy_id(int policy_id) {
        this.policy_id = policy_id;
    }



    public String getPolicyStartDate() {
        return policyStartDate;
    }

    public void setPolicyStartDate(String policyStartDate) {
        this.policyStartDate = policyStartDate;
    }

    public String getPolicyEndDate() {
        return policyEndDate;
    }

    public void setPolicyEndDate(String policyEndDate) {
        this.policyEndDate = policyEndDate;
    }


}

Address.java

package com.insubuy.model;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name="INSUBUY_CUSTOMER_ADDRESS")
public class Address {

    @Id
    @Column(name="ADDRESS_ID")
    private String address_id;

    public String getAddress_id() {
        return address_id;
    }
    public void setAddress_id(String address_id) {
        this.address_id = address_id;
    }
    @Column(name="ADDRESS_STREET")
    private String address_street;

    @Column(name="ADDRESS_CITY")
    private String address_city;

    @Column(name="ADDRESS_STATE")
    private String address_state;

    @Column(name="ADDRESS_ZIPCODE")
    private String address_zipcode;

    @GenericGenerator(name = "generator", strategy = "foreign",
    parameters = @Parameter(name = "property", value = "INSUBUY_CUSTOMER"))
    @GeneratedValue(generator = "generator")
    @Column(name="CUSTOMER_ID")
    private int customer_id;
    public String getAddress_street() {
        return address_street;
    }
    public void setAddress_street(String address_street) {
        this.address_street = address_street;
    }
    public String getAddress_city() {
        return address_city;
    }
    public void setAddress_city(String address_city) {
        this.address_city = address_city;
    }
    public String getAddress_state() {
        return address_state;
    }
    public void setAddress_state(String address_state) {
        this.address_state = address_state;
    }
    public String getAddress_zipcode() {
        return address_zipcode;
    }
    public void setAddress_zipcode(String address_zipcode) {
        this.address_zipcode = address_zipcode;
    }
    public int getCustomer_id() {
        return customer_id;
    }
    public void setCustomer_id(int customer_id) {
        this.customer_id = customer_id;
    }



}

Customer.java

package com.insubuy.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.*;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="INSUBUY_CUSTOMER")
public class Customer {

    private int customer_id;
    private String customer_name;
    private Date customer_dob;
    private Address address;
    private Policy policy;

    @OneToOne
    @JoinColumn(name = "CUSTOMER_ID")
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @OneToOne
    @JoinColumn(name = "CUSTOMER_ID")
    public Policy getPolicy() {
        return policy;
    }
    public void setPolicy(Policy policy) {
        this.policy = policy;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "CUSTOMER_ID")
    public int getCustomer_id() {
        return customer_id;
    }
    public void setCustomer_id(int customer_id) {
        this.customer_id = customer_id;
    }

    @Column(name = "CUSTOMER_NAME")
    public String getCustomer_name() {
        return customer_name;
    }
    public void setCustomer_name(String customer_name) {
        this.customer_name = customer_name;
    }

    @Column(name = "CUSTOMER_DOB")
    public Date getCustomer_dob() {
        return customer_dob;
    }
    public void setCustomer_dob(Date customer_dob) {
        this.customer_dob = customer_dob;
    }

}

这是主文件

package com.insubuy.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;



import com.insubuy.model.Address;
import com.insubuy.model.Customer;
import com.insubuy.model.Policy;
import org.aravind.util.HibernateUtil;

public class App {
    public static void main(String[] args) {
        System.out.println("Hibernate one to one (Annotation)");
        Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();

        Customer customer=new Customer();
        customer.setCustomer_id(3);
        customer.setCustomer_name("robert");
        Calendar myCal = Calendar.getInstance();
        myCal.set(Calendar.YEAR, 1988);
        myCal.set(Calendar.MONTH, 01);
        myCal.set(Calendar.DAY_OF_MONTH, 01);
        Date theDate = myCal.getTime();
        Address addr=new Address();
        Policy policy=new Policy();

        addr.setAddress_city("dallas");
        addr.setAddress_id("3");
        addr.setAddress_state("texas");
        addr.setAddress_street("xyz");
        addr.setAddress_zipcode("75125");

        policy.setPolicy_id(3333);
        policy.setPolicyEndDate("2018-01-01");
        policy.setPolicyStartDate("2017-01-01");

        customer.setAddress(addr);
        customer.setPolicy(policy);
        customer.setCustomer_dob(theDate);

        session.save(customer);
        session.getTransaction().commit();
        session.close();

        System.out.println("Done");
    }
}

我不确定为什么我得到customer_id没有默认值。任何帮助表示赞赏。

由于

1 个答案:

答案 0 :(得分:0)

问题在于以下代码。不确定为什么它没有生成sql语句的默认值。当我删除代码时,我能够插入数据库。

docker service ls