我尝试使用Hibernate和SQLite DB保存数据但是有SQLException:NYI

时间:2015-08-10 15:20:01

标签: java hibernate sqlite

当我尝试更新已存在的行时,一切都很好。只有save方法有问题。 我的地图类如下:

@Entity
@Table(name = "person")
public class Person{

    @Transient
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long personID = null;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @Column
    private Gender gender;

    @Column(name = "birthday")
    private Date birthday;
    /*
    @Column
    @Temporal(value=TemporalType.DATE)
    private Date dateOfDeath;
    */
    @Column
    private String education;

    @Column
    private String profession;

    @Column
    private String city;

    @Column
    private String otherInfo;

    @Column(name ="firstParentID")
    private Long firstParentID;

    @Column(name = "secondParentID")
    private Long secondParentID;

    public Person(){
    }

    public Person(Long parentID){
        this.firstParentID = parentID;
    }

    public Long getPersonID() {
        return personID;
    }

    public void setPersonID(Long personID) {
        this.personID = personID;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        propertyChangeSupport.firePropertyChange("firstName", this.firstName, this.firstName = firstName);
    }
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        propertyChangeSupport.firePropertyChange("lastName", this.lastName, this.lastName = lastName);
    }

    public String getGender() {
        if (gender != null)
            return gender.getDescription();
        return null;
    }
    public void setGender(String genderType) {
        propertyChangeSupport.firePropertyChange("gender", this.gender, this.gender = Gender.valueOf(genderType));
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        propertyChangeSupport.firePropertyChange("birthday", this.birthday, this.birthday = birthday);
    }
    /*
    public Date getDateOfDeath() {
        return dateOfDeath;
    }
    public void setDateOfDeath(Date dateOfDeath) {
        propertyChangeSupport.firePropertyChange("dateOfDeath", this.dateOfDeath, this.dateOfDeath = dateOfDeath);
    }
    */
    public String getEducation() {
        return education;
    }
    public void setEducation(String education) {
        propertyChangeSupport.firePropertyChange("education", this.education, this.education = education);
    }
    public String getProfession() {
        return profession;
    }
    public void setProfession(String profession) {
        propertyChangeSupport.firePropertyChange("profession", this.profession, this.profession = profession);
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        propertyChangeSupport.firePropertyChange("city", this.city, this.city = city);
    }
    public String getOtherInfo() {
        return otherInfo;
    }
    public void setOtherInfo(String otherInfo) {
        propertyChangeSupport.firePropertyChange("otherInfo", this.otherInfo, this.otherInfo = otherInfo);
    }
    public long getFirstParentID() {
        return firstParentID;
    }
    public void setFirstParentID(Long firstParentID) {
        propertyChangeSupport.firePropertyChange("firstParentID", this.firstParentID, this.firstParentID = firstParentID);
    }
    public long getSecondParentID() {
        return secondParentID;
    }
    public void setSecondParentID(Long secondParentID) {
        propertyChangeSupport.firePropertyChange("secondParentID", this.secondParentID, this.secondParentID = secondParentID);
    }

     public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
         propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
     }

     public void removePropertyChangeListener(PropertyChangeListener listener) {
         propertyChangeSupport.removePropertyChangeListener(listener);
     }

}

我尝试通过以下方法保存具有null personID的Person:

@Override
    public void savePerson(Person p) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(p);
            session.getTransaction().commit();
        }finally{
            if (session != null && session.isOpen())
                session.close();
        }
    }

但是我有SQLException和GenericJDBCException:无法准备语句。我将跳过细节并仅描述重要内容:

    ...
Caused by: org.hibernate.exception.GenericJDBCException: could not prepare 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.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:196)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:122)
    at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:55)
    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:492)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:334)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
    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 db.DAO.impl.PersonDAOImpl.savePerson(PersonDAOImpl.java:21)
...
    Caused by: java.sql.SQLException: NYI
    at org.sqlite.Conn.prepareStatement(Conn.java:217)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:124)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
    ... 61 more

hibernate.cfg.xml中:

   <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                             "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory name="">
      <property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
      <property name="hibernate.connection.url">jdbc:sqlite:C:\\TEST\MyDB</property>
      <property name="hibernate.connection.autocommit">false</property>
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
      <mapping class="people.Person"/>
     </session-factory>
    </hibernate-configuration>

2 个答案:

答案 0 :(得分:0)

Linkage 'Z' uses the same cluster twice。那里有多个SQLite驱动程序。确保使用Xerial driver而不是Zentus驱动程序,它不支持生成的ID。请参阅this链接。

此外,您用于SQLIte的 Dialect 不正确。有关详细信息,请参阅this SO线程。

答案 1 :(得分:0)

你使用的是正确的方言吗? 配置显示HSQL,您正在使用SQL Lite。 请检查并回复是否有效。您也可以查看一些定制的方言。