1

休止状態のフレームワークで問題が発生しました。

私には人がいて、人には複数の住所があるとしましょう。以下は私のコードです、

/* Person entity*/
@Entity
@Table(name="person")
public class Person implements Serializable{

    private static final long serialVersionUID = 7635130386543844954L;

    @Id
    @Column(name="person_id")
    private Integer personId;

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

    @Column(name="ic_no")
    private String icNo;

    @OneToMany(fetch=FetchType.EAGER)
    @JoinTable(name="address",
joinColumns{@JoinColumn(name="name")},inverseJoinColumns={@JoinColumn(name="address_id")})
    List<Address> listAddress = new ArrayList<Address>();

    public Integer getPersonId() {
        return personId;
    }

    public void setPersonId(Integer personId) {
        this.personId = personId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIcNo() {
        return icNo;
    }

    public void setIcNo(String icNo) {
        this.icNo = icNo;
    }

}

/* Address entity*/
@Entity
@Table(name="address")
public class Address implements Serializable{

    private static final long serialVersionUID = 1371825219482824135L;

    @Id
    @Column(name="address_id")
    private Integer addressId;

    @Column(name="address")
    private String address;

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

    public Integer getAddressId() {
        return addressId;
    }

    public void setAddressId(Integer addressId) {
        this.addressId = addressId;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


/* DAO code*/
List<ProductDetails>list = hibernateTemplate.find("from Person where Name=?",new Object[]{name});

プログラムを実行しようとすると、次のような例外が発生します。

Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not initialize a collection: [com.inventory.inventory.vo.Person.listAddress#1]; nested exception is org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.inventory.inventory.vo.Person.listAddress#1]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
    at com.inventory.inventory.dao.impl.ProductsDAOImpl.getProductsByCode(ProductsDAOImpl.java:28)
    at Main.main(Main.java:14)
Caused by: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.inventory.inventory.vo.Person.listAddress#1]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.loadCollection(Loader.java:2069)
    at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
    at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:628)
    at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
    at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1853)
    at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:476)
    at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:899)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:275)
    at org.hibernate.loader.Loader.doList(Loader.java:2294)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
    at org.hibernate.loader.Loader.list(Loader.java:2167)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:930)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
    ... 4 more

<b>Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = integer</b>

  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 305
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1849)
    at org.hibernate.loader.Loader.doQuery(Loader.java:718)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.loadCollection(Loader.java:2062)
    ... 21 more

私が異なる唯一のことは、文字データ型の列「名前」を使用してテーブルを結合し、逆結合は整数データ型であるアドレスIDを使用することです。とにかくビューオブジェクトを異なるデータ型でマップしますか?

4

1 に答える 1

2

これを試して

Person クラスを次のように変更します。

@OneToMany(cascade=mappedBy = "person", fetch = FetchType.EAGER)
private List<Address> listAddress = new ArrayList<Address>();

Address クラスを次のように変更します。

@JoinColumn(name = "person_id", referencedColumnName = "person_id")
@ManyToOne
private Person person;
于 2012-09-08T11:26:58.090 に答える