[英]Hibernate mapping map with @OneToMany bidirectional association
嘗試使用具有雙向關聯的實體時出現查詢問題。
我有一個實體錢包,該錢包與其他兩個實體有某些關系。 與實體WalletBranchOffice的關系存在問題。 如果我注釋了這部分代碼,一切都會很好。 這是我的實體:
Wallet.java
@Entity
@Table(name="WALLET", schema=SchemasConfig.SCHEMA_NEW)
@Cacheable(true)
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Wallet implements Serializable {
private static final long serialVersionUID = 3307006915060155334L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="WALLET_ID")
private Integer walletId;
public Integer getWalletId() {
return walletId;
}
@Column(name="INTERNAL_REFERENCE", nullable=false, length=32)
private String internalReference;
public String getInternalReference() {
return internalReference;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="CURRENCY_ID", nullable=false)
private Currency currency;
public Currency getCurrency() {
return currency;
}
@Column(name = "CACHE_AMOUNT_SUM", nullable = false, precision = 13, scale = 2)
private BigDecimal cacheAmountSum;
public BigDecimal getCacheAmountSum() {
return cacheAmountSum;
}
@OneToMany(mappedBy="wallet", fetch=FetchType.EAGER)
@MapKeyColumn(name="BRANCH_OFFICE_ID")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
private Map<Integer, WalletBranchOffice> walletBranchOffices;
public Map<Integer, WalletBranchOffice> getWalletBranchOffices() {
return walletBranchOffices;
}
@OneToMany(mappedBy="wallet", fetch=FetchType.EAGER)
@MapKeyColumn(name="WALLET_PREV_ID")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
/*
* wallets which must be used before current wallet
*/
private Map<Integer, WalletDependency> walletDependencies;
public Map<Integer, WalletDependency> getWalletDependencies() {
return walletDependencies;
}
@OneToMany(mappedBy="walletPrev", fetch=FetchType.EAGER)
@MapKeyColumn(name="WALLET_ID")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
/*
* wallets that can be used after current wallet
*/
private Map<Integer, WalletDependency> dependentWallets;
public Map<Integer, WalletDependency> getDependentWallets() {
return dependentWallets;
}
@Column(name = "TEXT_KEY")
private String textKey;
public String getTextKey() {
return textKey;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((walletId == null) ? 0 : walletId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Wallet other = (Wallet) obj;
if (walletId == null) {
if (other.walletId != null)
return false;
} else if (!walletId.equals(other.walletId))
return false;
return true;
}
}
WalletBranchOffice.java
@Entity
@Table(name="WALLET_BRANCH_OFFICE", schema=SchemasConfig.SCHEMA_NEW)
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
@Immutable
public class WalletBranchOffice implements Serializable {
private static final long serialVersionUID = 9135909966091486878L;
@Id
@Column(name="WALLET_BRANCH_OFFICE_ID")
private Integer walletBranchOfficeId;
public Integer getWalletBranchOfficeId() {
return walletBranchOfficeId;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="WALLET_ID", nullable=false)
private Wallet wallet;
public Wallet getWallet() {
return wallet;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="BRANCH_OFFICE_ID", nullable=false)
private BranchOffice branchOffice;
public BranchOffice getBranchOffice() {
return branchOffice;
}
@Column(name="CREATE DATETIME")
@Type(type="com.uniqagroup.ims.common.EETDateType$TimestampType")
private Timestamp createDatetime;
public Timestamp getCreateDatetime() {
return createDatetime;
}
}
這是WalletDependency.java,沒有問題。
@Entity
@Table(name="WALLET_DEPENDENCY",schema=SchemasConfig.SCHEMA_NEW)
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
@Immutable
public class WalletDependency implements Serializable {
private static final long serialVersionUID = -6665047739101469610L;
@Id
@Column(name="WALLET_DEPENDENCY_ID")
private Integer walletRequiredId;
public Integer getWalletRequiredId() {
return walletRequiredId;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="WALLET_ID", nullable=false)
private Wallet wallet;
public Wallet getWallet() {
return wallet;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="WALLET_PREV_ID",nullable=false)
private Wallet walletPrev;
public Wallet getWalletPrev() {
return walletPrev;
}
}
我的問題是,即使我執行一個簡單的選擇查詢,例如:
List<Wallet> wallets = em.createQuery("FROM Wallet AS w",Wallet.class).getResultList();
與EntityManager,我得到一個SQL語法錯誤:
Caused by: java.sql.SQLSyntaxErrorException: [SQL0199] Keyword AS not expected. Valid tokens: , FROM INTO.
at com.ibm.as400.access.JDError.createSQLExceptionSubClass(JDError.java:852) [jt400-7.9_jdbc4.0.jar:JTOpen 7.9]
at com.ibm.as400.access.JDError.throwSQLException(JDError.java:692) [jt400-7.9_jdbc4.0.jar:JTOpen 7.9]
at com.ibm.as400.access.JDError.throwSQLException(JDError.java:662) [jt400-7.9_jdbc4.0.jar:JTOpen 7.9]
at com.ibm.as400.access.AS400JDBCStatement.commonPrepare(AS400JDBCStatement.java:1763) [jt400-7.9_jdbc4.0.jar:JTOpen 7.9]
at com.ibm.as400.access.AS400JDBCPreparedStatement.<init>(AS400JDBCPreparedStatement.java:354) [jt400-7.9_jdbc4.0.jar:JTOpen 7.9]
at com.ibm.as400.access.AS400JDBCConnection.prepareStatement(AS400JDBCConnection.java:2166) [jt400-7.9_jdbc4.0.jar:JTOpen 7.9]
at com.ibm.as400.access.AS400JDBCConnection.prepareStatement(AS400JDBCConnection.java:2108) [jt400-7.9_jdbc4.0.jar:JTOpen 7.9]
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.doPrepareStatement(BaseWrapperManagedConnection.java:732)
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.prepareStatement(BaseWrapperManagedConnection.java:707)
at org.jboss.jca.adapters.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:404)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:161) [hibernate-core-4.2.0.Final-redhat-1.jar:4.2.0.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182) [hibernate-core-4.2.0.Final-redhat-1.jar:4.2.0.Final-redhat-1]
... 163 more
無需AS
:
List<Wallet> wallets = em.createQuery("FROM Wallet w",Wallet.class).getResultList();
一些例子:
謝謝您的重播!!! 打印出生成的SQL后,我發現了問題。 這只是一個愚蠢的技術錯誤。 在WalletBranchOffice實體中將createDatetime屬性映射到create_datetime列時缺少“ _”符號。 @Column(name =“ CREATE DATETIME”)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.