Java 在hibernate中从多个表检索数据
我有两个表,即订户和联系人。 表如下所示:Java 在hibernate中从多个表检索数据,java,database,hibernate,multiple-tables,Java,Database,Hibernate,Multiple Tables,我有两个表,即订户和联系人。 表如下所示: subscriber -> id, contact_id //contact_id is a foreign key contact -> id, firstName, lastName, email, contactType <hibernate-mapping> <class name="com.DBNAME.model.Contact" table="contact" > <i
subscriber -> id, contact_id //contact_id is a foreign key
contact -> id, firstName, lastName, email, contactType
<hibernate-mapping>
<class name="com.DBNAME.model.Contact" table="contact" >
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="contactType" type="int">
<column name="contactType" sql-type="TINYINT"></column>
</property>
<property name="firstName" type="string">
<column name="firstName"></column>
</property>
<property name="lastName" type="string">
<column name="lastName"></column>
</property>
</class>
</hibernate-mapping>
我的Contact.hbm.xml文件如下所示:
subscriber -> id, contact_id //contact_id is a foreign key
contact -> id, firstName, lastName, email, contactType
<hibernate-mapping>
<class name="com.DBNAME.model.Contact" table="contact" >
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="contactType" type="int">
<column name="contactType" sql-type="TINYINT"></column>
</property>
<property name="firstName" type="string">
<column name="firstName"></column>
</property>
<property name="lastName" type="string">
<column name="lastName"></column>
</property>
</class>
</hibernate-mapping>
Hibernate生成的查询如下所示:
<hibernate-mapping>
<class name="com.DBNAME.model.Subscriber" table="subscriber" >
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="contact" class="com.DBNAME.model.Contact" column="contact_id" unique="true" fetch="join"/>
</class>
</hibernate-mapping>
select subscriber0_.id as id1_, subscriber0_.contact_id as contact2_1_ from subscriber subscriber0_
我没有从联系人表中获取联系人详细信息。我怎样才能做到这一点 尝试使用以下方法:
<many-to-one name="contact"
class="com.DBNAME.model.Contact" column="contact_id"
unique="true" lazy="false"/>
例如,
lazy=“false”
和nofetch
属性。联系人对象为空,联系人内的所有详细信息都设置为空。在.hbm文件中使用“lazy=false”。这在您的情况下是正确的,因为您得到的是空条目。使用此选项后,它将在subscriber.org.hibernate.PropertyAccessException中保留数据:为com.smackdab.model.Contact.shipAddressIdI的基元类型setter属性分配了Null值。我在联系人表中也有此shipAddressId。现在它是空的如果将Null值分配给基元类型的属性
,则表示contactType
或contactId
列很可能包含Null值。尝试将其类型更改为整数。爱你的快速答案!兄弟般的爱!:D但是你是怎么发现懒惰=虚假的东西的?我不明白。顺便说一句,现在我将字段从int改为string,这样它就可以接受所有字段为null!;)您还可以将其更改为整数
。只是有很多关于Hibernate的经验:)