我正在尝试学习如何使用hibernate并遇到一个问题,我已经坚持了一段时间。
我正在尝试将两个表连接在一起,并以GUI形式显示值。我已经尝试过研究答案,但我的案例似乎与其他人列出的不同。这似乎是每个表的ID之间的关系的问题。
这是我可以显示任何结果的唯一声明,但它基本上显示了两次相同的GameTemplates
表。
尽我所能:
from GameTemplates gt join gt.id.partNum Tickets
所需的SQL:
select * from Game_Templates
inner join Tickets
where
Game_Templates.part_Num = Tickets.game_Templates_Part_Num
GameTemplates.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2015 7:01:43 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="entity.GameTemplates" table="game_templates" catalog="fire_tickets">
<composite-id name="id" class="entity.GameTemplatesId">
<key-property name="partNum" type="string">
<column name="part_num" length="12" />
</key-property>
<key-property name="distsIdDistId" type="int">
<column name="dists_id_dist_id" />
</key-property>
<key-property name="mfgIdMfgId" type="int">
<column name="mfg_id_mfg_id" />
</key-property>
</composite-id>
<many-to-one name="distsId" class="entity.DistsId" update="false" insert="false" fetch="select">
<column name="dists_id_dist_id" not-null="true" />
</many-to-one>
<many-to-one name="mfgId" class="entity.MfgId" update="false" insert="false" fetch="select">
<column name="mfg_id_mfg_id" not-null="true" />
</many-to-one>
<property name="gameName" type="string">
<column name="game_name" length="20" />
</property>
<property name="gameCost" type="java.lang.Double">
<column name="game_cost" precision="22" scale="0" />
</property>
<property name="ticketCost" type="java.lang.Double">
<column name="ticket_cost" precision="22" scale="0" />
</property>
<property name="numTickets" type="java.lang.Integer">
<column name="num_tickets" />
</property>
<property name="ideaGross" type="java.lang.Integer">
<column name="idea_gross" />
</property>
<property name="ideaPrizes" type="java.lang.Integer">
<column name="idea_prizes" />
</property>
<property name="ideaNet" type="java.lang.Integer">
<column name="idea_net" />
</property>
<property name="lastSale" type="java.lang.Integer">
<column name="Last_sale" />
</property>
<property name="lastSaleAllow" type="java.lang.Integer">
<column name="Last_sale_allow" />
</property>
<property name="prizeAmt1" type="java.lang.Integer">
<column name="Prize_amt1" />
</property>
<property name="prizeAmt2" type="java.lang.Integer">
<column name="Prize_amt2" />
</property>
<property name="prizeAmt3" type="java.lang.Integer">
<column name="Prize_amt3" />
</property>
<property name="prizeAmt4" type="java.lang.Integer">
<column name="Prize_amt4" />
</property>
<property name="prizeAmt5" type="java.lang.Integer">
<column name="Prize_amt5" />
</property>
<property name="prizeAmt6" type="java.lang.Integer">
<column name="Prize_amt6" />
</property>
<property name="prizeAmt7" type="java.lang.Integer">
<column name="Prize_amt7" />
</property>
<property name="prizeAmt8" type="java.lang.Integer">
<column name="Prize_amt8" />
</property>
<property name="prizeAmt9" type="java.lang.Integer">
<column name="Prize_amt9" />
</property>
<property name="prizeAmt10" type="java.lang.Integer">
<column name="Prize_amt10" />
</property>
<property name="prizeAmt11" type="java.lang.Integer">
<column name="Prize_amt11" />
</property>
<property name="prizeAmt12" type="java.lang.Integer">
<column name="Prize_amt12" />
</property>
<property name="prizeAmt13" type="java.lang.Integer">
<column name="Prize_amt13" />
</property>
<property name="prizeAmt14" type="java.lang.Integer">
<column name="Prize_amt14" />
</property>
<property name="prizeAmt15" type="java.lang.Integer">
<column name="Prize_amt15" />
</property>
<property name="prizeAll1" type="java.lang.Integer">
<column name="Prize_all1" />
</property>
<property name="prizeAll2" type="java.lang.Integer">
<column name="Prize_all2" />
</property>
<property name="prizeAll3" type="java.lang.Integer">
<column name="Prize_all3" />
</property>
<property name="prizeAll4" type="java.lang.Integer">
<column name="Prize_all4" />
</property>
<property name="prizeAll5" type="java.lang.Integer">
<column name="Prize_all5" />
</property>
<property name="prizeAll6" type="java.lang.Integer">
<column name="Prize_all6" />
</property>
<property name="prizeAll7" type="java.lang.Integer">
<column name="Prize_all7" />
</property>
<property name="prizeAll8" type="java.lang.Integer">
<column name="Prize_all8" />
</property>
<property name="prizeAll9" type="java.lang.Integer">
<column name="Prize_all9" />
</property>
<property name="prizeAll10" type="java.lang.Integer">
<column name="Prize_all10" />
</property>
<property name="prizeAll11" type="java.lang.Integer">
<column name="Prize_all11" />
</property>
<property name="prizeAll12" type="java.lang.Integer">
<column name="Prize_all12" />
</property>
<property name="prizeAll13" type="java.lang.Integer">
<column name="Prize_all13" />
</property>
<property name="prizeAll14" type="java.lang.Integer">
<column name="Prize_all14" />
</property>
<property name="prizeAll15" type="java.lang.Integer">
<column name="Prize_all15" />
</property>
</class>
</hibernate-mapping>
Tickets.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2015 7:01:43 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="entity.Tickets" table="tickets" catalog="fire_tickets">
<composite-id name="id" class="entity.TicketsId">
<key-property name="serial" type="string">
<column name="Serial" length="12" />
</key-property>
<key-property name="gameTemplatesPartNum" type="string">
<column name="game_templates_part_num" length="12" />
</key-property>
</composite-id>
<property name="datePlaced" type="date">
<column name="Date_placed" length="10" />
</property>
<property name="dateRemoved" type="date">
<column name="Date_removed" length="10" />
</property>
<property name="unsoldAmt" type="java.lang.Integer">
<column name="Unsold_amt" />
</property>
<property name="actualGross" type="java.lang.Integer">
<column name="Actual_gross" />
</property>
<property name="actualPrizes" type="java.lang.Integer">
<column name="Actual_prizes" />
</property>
<property name="actualNet" type="java.lang.Integer">
<column name="Actual_net" />
</property>
<property name="bin" type="java.lang.Integer">
<column name="Bin" />
</property>
<property name="inplay" type="java.lang.Boolean">
<column name="Inplay" />
</property>
<property name="closed" type="java.lang.Integer">
<column name="Closed" />
</property>
<property name="unsoldTickets" type="java.lang.Integer">
<column name="Unsold_tickets" />
</property>
<property name="lastSaleRem" type="java.lang.Byte">
<column name="Last_sale_rem" />
</property>
<property name="prizeRem1" type="java.lang.Integer">
<column name="Prize_rem1" />
</property>
<property name="prizeRem2" type="java.lang.Integer">
<column name="Prize_rem2" />
</property>
<property name="prizeRem3" type="java.lang.Integer">
<column name="Prize_rem3" />
</property>
<property name="prizeRem4" type="java.lang.Integer">
<column name="Prize_rem4" />
</property>
<property name="prizeRem5" type="java.lang.Integer">
<column name="Prize_rem5" />
</property>
<property name="prizeRem6" type="java.lang.Integer">
<column name="Prize_rem6" />
</property>
<property name="prizeRem7" type="java.lang.Integer">
<column name="Prize_rem7" />
</property>
<property name="prizeRem8" type="java.lang.Integer">
<column name="Prize_rem8" />
</property>
<property name="prizeRem9" type="java.lang.Integer">
<column name="Prize_rem9" />
</property>
<property name="prizeRem10" type="java.lang.Integer">
<column name="Prize_rem10" />
</property>
<property name="prizeRem11" type="java.lang.Integer">
<column name="Prize_rem11" />
</property>
<property name="prizeRem12" type="java.lang.Integer">
<column name="Prize_rem12" />
</property>
<property name="prizeRem13" type="java.lang.Integer">
<column name="Prize_rem13" />
</property>
<property name="prizeRem14" type="java.lang.Integer">
<column name="Prize_rem14" />
</property>
<property name="prizeRem15" type="java.lang.Integer">
<column name="Prize_rem15" />
</property>
<property name="datePurch" type="date">
<column name="date_purch" length="10" />
</property>
<property name="invoiceNum" type="string">
<column name="invoice_num" length="10" />
</property>
</class>
</hibernate-mapping>
答案 0 :(得分:0)
试试这个:
from GameTemplates gt, Tickets t where gt.id.partNum = t.id.gameTemplatesPartNum