[英]Issue with Hibernate many to many mapping
我有一個休眠+ oracle程序。 這樣,我就可以通過“ ApplicationIdentityProviders”表在“ Applications”和“ IdentityProviders”表之間建立多對多關系。 運行程序時出現以下錯誤:
Exception in thread "main" java.lang.RuntimeException: org.hibernate.MappingException:
Could not determine type for: String, at table: identity_providers, for columns: [org.hibernate.mapping.Column(issuer)]
at com.oracle.hibernate.OCIAuthManager.setupTenant1Pdb1(OCIAuthManager.java:246)
at com.oracle.hibernate.OCIAuthManager.main(OCIAuthManager.java:283)
我不知道為什么。 Applications.java在這里 。 IdentityProviders.java在這里 。 ApplicationIdentityProviders.java在這里 。 主班在這里 。 Applications.hbm.xml如下:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "Applications" table = "applications">
<meta attribute = "class-description">
This class contains the applications details.
</meta>
<id name = "application_id" type = "int" column = "application_id">
<generator class="native"/>
</id>
<set name = "application_id_providers" cascade="save-update" table="application_identity_providers">
<key column = "application_id"/>
<many-to-many column = "identity_provider_id" class="IdentityProviders"/>
</set>
<property name = "application_name" column = "application_name" type = "string"/>
<property name = "attr" column = "attr" type = "short"/>
<property name = "salary" column = "salary" type = "int"/>
</class>
<class name = "IdentityProviders" table = "identity_providers">
<meta attribute = "class-description">
This class contains the identity providers' details.
</meta>
<id name = "identity_provider_id" type = "int" column = "identity_provider_id">
<generator class="native"/>
</id>
<property name = "attr" column = "attr" type = "short"/>
<property name = "protocols" column = "protocols" type = "short"/>
<property name = "issuer" column = "issuer" type = "String"/>
<property name = "cert1" column = "cert1" type = "String"/>
<property name = "cert2" column = "cert2" type = "String"/>
<property name = "is_tenant_default" column = "is_tenant_default" type = "short"/>
</class>
</hibernate-mapping>
hibernate-1.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>
<property name="connection.url">jdbc:oracle:thin:@//localhost:1521/t1p1.oradev.oraclecorp.com</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.Oracle12cDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<mapping resource = "Applications.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我要去哪里錯了? 請幫助。
更新1:@Guillaume的答案有效。 但是我現在收到以下錯誤:
Exception in thread "main" java.lang.RuntimeException:
org.hibernate.MappingException: An association from the table
application_identity_providers refers to an unmapped class:
IdentityProviders at
com.oracle.hibernate.OCIAuthManager.setupTenant1Pdb1(OCIAuthManager.java:246)
at com.oracle.hibernate.OCIAuthManager.main(OCIAuthManager.java:283)
更新2:我解決了更新1中的錯誤。現在,我得到以下錯誤:錯誤:ORA-01400:無法將NULL插入(“ SYS”。“ APPLICATION_IDENTITY_PROVIDERS”。“ APPLICATION_IDENTITY_PROVIDER_ID”)
此錯誤發生在具有其他兩個表的多表映射關系的主表的中間表中。 在ApplicationIdentityProviders.java中,我具有:
@Id
@SequenceGenerator(name="seq",sequenceName="oracle_seq",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
private int application_identity_provider_id;
當我運行程序時,相關的輸出片段為:Hibernate:
create table application_identity_providers (
application_identity_provider_id number(10,0) not null,
application_id number(10,0) not null,
identity_provider_id number(10,0) not null,
is_application_default number(5,0) not null,
primary key (application_id, identity_provider_id)
)
我要去哪里錯了?
更新3:在“應用程序”表中給定“ application_name”,在“ identity_providers”和“ saml_identity_providers”表中獲取全部內容。 怎么做?
我嘗試了以下方法:
Query query = entitymanager.createQuery("select a from Applications a join
fetch a.ApplicationIdentityProviders ap " +
"join fetch ap.IdentityProviders ip where a.application_name =
'newApplicationName'");
它給了我這個錯誤:
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'applicationsRepository' defined in file
[D:\filepath\ApplicationsRepository.class]: Bean instantiation via
constructor failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate
[ApplicationsRepository]: Constructor threw exception; nested exception is
java.lang.IllegalArgumentException: org.hibernate.QueryException: could not
resolve property: ApplicationIdentityProviders of: Applications [select a
from com.package.entities.Applications a join fetch
a.ApplicationIdentityProviders ap join fetch
ap.IdentityProviders ip where a.application_name = 'newApplicationName']
我認為對於這些行,您的類型應為小寫字符串(而不是String):
<property name = "issuer" column = "issuer" type = "string"/>
<property name = "cert1" column = "cert1" type = "string"/>
<property name = "cert2" column = "cert2" type = "string"/>
我認為您還需要在映射文件中聲明Java類的限定名稱,這可以通過在休眠映射元素中添加packagename來完成:
<hibernate-mapping package="com.oracle.hibernate">
更快的方法是使用Hibernate工具自動生成映射文件。 請參考以下文章。
如何在Eclipse中安裝Hibernate Tools?
http://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/
在Application.java中
刪除以下行:
private Set<IdentityProviders> application_id_providers;
添加以下行:
@ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "application_identity_providers",
joinColumns = @JoinColumn(name = "application_id"),
inverseJoinColumns = @JoinColumn(name = "identity_provider_id")
)
private Set<IdentityProviders> id_providers;
在IdentityProviders.java中
添加以下行:
@ManyToMany(mappedBy = "id_providers")
private Set<Applications> applications;
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<SamlIdentityProviders> si_providers;
然后查詢可能是:
SELECT a from Applications a JOIN fetch a.id_providers ip JOIN fetch ip.si_providers sip
WHERE a.application_name = 'newApplicationName' AND ip.issuer = 'issuerName'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.