2012-03-07 65 views
2

used:hibernate 3.6.2,maven 2,postgres 9. 我有代碼必須工作,但它不。當我啓動功能測試,我得到了錯誤:java.lang.ClassCastException:org.hibernate.action.DelayedPostInsertIdentifier不能轉換爲java.lang.Long

java.lang.ClassCastException:org.hibernate.action.DelayedPostInsertIdentifier不能轉換爲java.lang.Long中

代碼是非標準的域模型:

實體:

@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL) 
@Entity 
@Table(schema = "simulators", name = "mySimulator_card") 
public class MySimulatorCard { 

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

    @Column(name = "account_number", unique = true, nullable = false) 
    private String accountNumber; 

等等

DAO:

public abstract class AbstractDao<E, PK extends Serializable> implements Dao<E, PK> { 

    private EntityManager entityManager; 

    public EntityManager getEntityManager() { 
     return entityManager; 
    } 

    @PersistenceContext(unitName = "MySimulator") 
    public void setEntityManager(EntityManager entityManager) { 
     this.entityManager = entityManager; 
    } 

    public abstract Class<E> getEntityClass(); 

    @Override 
    public void persist(E e) { 
     getEntityManager().persist(e); 
    } 

    @Override 
    public E merge(E e) { 
     return getEntityManager().merge(e); 
    } 

    @Override 
    public void refresh(E e) { 
     getEntityManager().refresh(e); //<-- some thing wroooong 
    } 

    @Override 
    public void delete(E e) { 
     getEntityManager().remove(e); 
    } 

等等

而且根據表:

CREATE TABLE simulators.mySimulator_card 
(
    id bigserial NOT NULL, 
    account_number character varying(255) NOT NULL, 

etc... 

    CONSTRAINT mySimulator_card_pk PRIMARY KEY (id), 
    CONSTRAINT mySimulator_card_account_fk FOREIGN KEY (account_id) 
     REFERENCES simulators.mySimulator_account (id) MATCH SIMPLE 
     ON UPDATE NO ACTION ON DELETE NO ACTION, 
    CONSTRAINT mySimulator_card_currency_fk FOREIGN KEY (currency_id) 
     REFERENCES simulators.mySimulator_currency ("name") MATCH SIMPLE 
     ON UPDATE NO ACTION ON DELETE NO ACTION, 
    CONSTRAINT mySimulator_card_product_fk FOREIGN KEY (product_id) 
     REFERENCES simulators.mySimulator_product (id) MATCH SIMPLE 
     ON UPDATE NO ACTION ON DELETE NO ACTION, 
    CONSTRAINT mySimulator_account_account_number_uq UNIQUE (account_number), 
    CONSTRAINT mySimulator_card_san_uq UNIQUE (san) 
) 
WITH (
    OIDS=FALSE 
); 
ALTER TABLE simulators.mySimulator_card OWNER TO functional; 

下面是堆棧跟蹤,直到我的代碼:

at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:36) 
     at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:52) 
     at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:91) 
     at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:282) 
     at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:277) 
     at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1873) 
     at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1844) 
     at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1716) 
     at org.hibernate.loader.Loader.doQuery(Loader.java:801) 
     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) 
     at org.hibernate.loader.Loader.loadEntity(Loader.java:2037) 
     at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86) 
     at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76) 
     at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3293) 
     at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:151) 
     at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:62) 
     at org.hibernate.impl.SessionImpl.fireRefresh(SessionImpl.java:1118) 
     at org.hibernate.impl.SessionImpl.refresh(SessionImpl.java:1098) 
     at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:738) 
     at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:713) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:601) 
     at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) 
     at $Proxy153.refresh(Unknown Source) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:601) 
     at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) 
     at $Proxy82.refresh(Unknown Source) 
     at com.goooogle.simulator.mysimulator.dao.AbstractDao.refresh(AbstractDao.java:42) 

爲什麼?它是休眠的錯誤?

回答

0

你沒有定義主鍵的IDENTITY你的創建表語句。

id bigserial NOT NULL IDENTITY 
+0

我不是postgres中的guru,但是在postgreSQL中沒有關鍵字標識,所以我無法使用你的例子。 – 2012-03-07 11:26:07

+0

嘗試創建一個序列。 CREATE SEQUENCE sq; CREATE TABLE t_test( id INTEGER PRIMARY KEY DEFAULT NEXTVAL('sq'), name VARCHAR(10) ); INSERT INTO t_test(name) VALUES('Andrew'); INSERT INTO t_test(name) VALUES('Gordon'); SELECT * FROM t_test; – 2012-03-07 11:29:02

+0

選中此項:http://sqlzoo.net/howto/source/z.dir/tip000001/postgres – 2012-03-07 11:30:13

1

我在執行save()時遇到了完全相同的異常。我通過更正hibernate映射文件中的級聯選項來解決它。我把它從「全刪除孤立,保存更新,刪除」更改爲「保存更新,刪除,刪除孤兒」,它爲我工作。 希望它有幫助。

相關問題