如何在java中的DaoImpl类中设置类型Date

时间:2014-03-14 12:12:19

标签: java postgresql spring-mvc

使用spring mvc将类型日期发布到postgresql 我在DealPosted类中有一对多的关系与另一个table.dealcatid是这个类中的外键。我怎么能得到那个白痴?

这是我的bean类

package com.saveDollar.model;



import java.io.Serializable;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;

@Entity
@Table(name="tbldeals_posted")

public class DealPosted implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    int DealID;
    String PostedBy;
    String DeaTitle;
    String DealDesc;
    int Days;
    Time FromTime;
    Time ToTime;
    SimpleDateFormat formatter=new SimpleDateFormat("dd-mm-yy");
    Date StartDate;
    Date EndDate;
    boolean is_deleted;

    private Set<DealCategory> dealCategory = new HashSet<DealCategory>(0);
    private Set<City> city=new HashSet<City>(0);

    public DealPosted()
     {

     }

     public DealPosted(String PostedBy,String DeaTitle,String DealDesc,Set<City> city,
             Date StartDate,Date EndDate,int Days,Time FromTime,Time ToTime,boolean is_deleted,
             Set<DealCategory>  dealCategory)
     {
         this.dealCategory=dealCategory;
         this.PostedBy=PostedBy;
         this.DeaTitle=DeaTitle;
         this.DealDesc=DealDesc;
         this.StartDate=StartDate;
         this.EndDate=EndDate;
         this.Days=Days;
         this.FromTime=FromTime;
         this.ToTime=ToTime;
         this.city=city;
         this.is_deleted=is_deleted;
    }
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="dealid")
    public int getDealID() 
    {
        return DealID;
    }

    public void setDealID(int dealID) 
    {
        DealID = dealID;
    }
    @Column(name="postedby")
    public String getPostedBy() 
    {
        return PostedBy;
    }

    public void setPostedBy(String postedBy)
    {
        PostedBy = postedBy;
    }

    @Column(name="deatitle")
    public String getDeaTitle() {
        return DeaTitle;
    }


    @Column(name="is_deleted")
    public boolean getIs_deleted(boolean is_deleted)
    {
        return is_deleted;
    }

    public void setIs_deleted(boolean is_deleted) 
    {
        this.is_deleted = is_deleted;
    }

    public void setDeaTitle(String deaTitle) 
    {
        DeaTitle = deaTitle;
    }
    @Column(name="dealdesc")
    public String getDealDesc() {
        return DealDesc;
    }

    public void setDealDesc(String dealDesc) 
    {
        DealDesc = dealDesc;
    }
    @Column(name="startdate")
    public Date getStartDate() 
    {
        return StartDate;
    }
    public void setStartDate(Date startDate)
    {
        StartDate = startDate;
    }
    @Column(name="enddate")
    public Date getEndDate() 
    {
        return EndDate;
    }

    public void setEndDate(Date endDate) 
    {
        EndDate = endDate;
    }
    @Column(name="days")
    public int getDays() {
        return Days;
    }

    public void setDays(int days) {
        Days = days;
    }
    @Column(name="fromtime")
    public Time getFromTime() {
        return FromTime;
    }

    public void setFromTime(Time fromTime) {
        FromTime = fromTime;
    }
    @Column(name="totime")
    public Time getToTime() {
        return ToTime;
    }

    public void setToTime(Time toTime) 
    {
        ToTime = toTime;
    }

    /*@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
    @JoinTable(name="city",joinColumns={@JoinColumn(name="cityid")})*/

    @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="cityid")
    public Set<City> getCity() 
    {
        return city;
    }
    public void setCity(Set<City> city) 
    {
        this.city = city;
    }
    @OneToMany(fetch=FetchType.EAGER,mappedBy = "dealcatid",cascade=CascadeType.ALL)
     public Set<DealCategory> getDealCategory() 
     {
        return dealCategory;
     }
    public void setDealCategory(Set<DealCategory> dealCategory)
    {
        this.dealCategory=dealCategory;
    }
}


this is my DealDaoImpl class

     public class DealDaoImpl implements DealDao   
        {


            private SessionFactory sessionFactory;
            private Session session=null;

            @Autowired
            public void setSessionFactory(SessionFactory sessionFactory) 
            {
                this.sessionFactory = sessionFactory;
            }
            @SuppressWarnings("unchecked")
            @Override
            public List<DealPosted> getAllDeals() 
            {
                session =sessionFactory.getCurrentSession();
                 List<DealPosted> deals =  (List<DealPosted>) session.
                         createSQLQuery("select * from tbldeals_posted d")
                         .addEntity("d", DealPosted.class)
                         .list();
                 return deals;
            }
            @Override
            public int addDeal(DealPosted dealPosted) 
            {
                final Calendar calendar = new GregorianCalendar();
                dealPosted.setStartDate(calendar.getTime());

                dealPosted.setDealID(3);
                dealPosted.setPostedBy("Rama");
                dealPosted.setDeaTitle("pizza");

                dealPosted.setEndDate(calendar.getTime());
                dealPosted.setDealDesc("abc");
                dealPosted.setDays(4);

                session=sessionFactory.getCurrentSession();
                session.save(dealPosted);
                return dealPosted.getDealID();
            }

             Hibernate: insert into tbldeals_posted (days, postedby, deatitle, dealdesc, startdate, enddate, fromtime, totime) values (?, ?, ?, ?, ?, ?, ?, ?)
            2014-03-14 18:33:32 WARN  JDBCExceptionReporter:77 - SQL Error: 0, SQLState: 23502
            2014-03-14 18:33:32 ERROR JDBCExceptionReporter:78 - ERROR: null value in column "dealcatid" violates not-null constraint
              Detail: Failing row contains (11, null, Rama                                              , pizza                                             , abc, null, null, null, null, 4, null, null, null).

我无法解决问题。 就此问题提出任何建议 感谢

1 个答案:

答案 0 :(得分:0)

在代码剪辑中,您发布了没有初始化或给“startDate”任何值的地方。如果要为该字段设置一些任意值,请使用GregorianCalendar。例如,这将为您设置当前日期:

final Calendar calendar = new GregorianCalendar();
dealPosted.setStartDate(calendar.getTime());