JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping » Hibernate  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 hibernate 對上 sql server問題
sample33





發文: 7
積分: 0
於 2015-05-16 02:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Table
Msg ---> Msgid Topicid

Topic ---> Topicid TopicName

我讓hibernate直接長出來 大概是

Msg hbm
<class name="coffeeBean.Msg" table="MSG" catalog="DBofDB" optimistic-lock="version">
<id name="msgId" type="int">
<column name="MSG_ID" />
<generator class="assigned" />
</id>
<many-to-one name="msgTopic" class="coffeeBean.MsgTopic" fetch="select">
<column name="MSG_TOPIC_ID" not-null="true" />
</many-to-one> ...............
Msg class
public class Msg {

  private int msgId;
  private MsgTopic msgTopic;
......

Topic hbm
<class name="coffeeBean.MsgTopic" table="MSG_TOPIC" catalog="DBofDB" optimistic-lock="version">
<id name="msgTopicId" type="int">
<column name="MSG_TOPIC_ID" />
<generator class="assigned" />
</id>
<property name="topicName" type="serializable">
<column name="TOPIC_NAME" not-null="true" />
</property>
<set name="msgs" table="MSG" inverse="true" lazy="true" fetch="select">
<key>
<column name="MSG_TOPIC_ID" not-null="true" />
</key>
<one-to-many class="coffeeBean.Msg" />
</set>
</class>

Topic class

public class MsgTopic {

  private int msgTopicId;
  private String topicName;
  private Set msgs = new HashSet(0);

  public MsgTopic() {
  }........

請問 能夠使用他提供的這些 能夠像sql 裡面的
select TOPICNAME from MSG m1 join MSGTOPIC m2 on m1.MSGTOPICID=m2.MSGTOPICID
讓我能夠直接從msg的bean中 取得 關聯table topic 中的topicName嗎
或是其他方法 也能夠產生類似功能


sample33 edited on 2015-05-16 02:37
reply to postreply to post
作者 Re:hibernate 對上 sql server問題 [Re:sample33]
sample33





發文: 7
積分: 0
於 2015-05-16 06:40 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我知道的方法只有一般ORM
因此我1個object對應1個table

session 由spring 注入..

public List<Msg> selectbytopicid(int topicid) {
    MsgTopic bean = (MsgTopic) getSession().get(MsgTopic.class, topicid);
    System.out.println(bean.getTopicName());
    List<Msg>query2 =new ArrayList<Msg>();
    Query query1 = getSession().createQuery("from Msg");
    
    Iterator<Msg>m1 =query1.iterate();
    while(m1.hasNext()){
      Msg m2 =m1.next();
      m2.setTopicName(bean.getTopicName());
      query2.add(m2);
    }
    return (List<Msg> ) query2;
}

想法是分開產生 再塞入 這方法可行嗎?

hbm就很一般的是

<class name="coffeeBean.Msg" table="MSG">
<id name="msgId" type="int">
<column name="MSG_ID" />
<generator class="native" />
</id>

<property name="msgTopicId" type="int">
<column name="MSG_TOPIC_ID" not-null="true" />
</property>
<property name="msgTime" type="date">
<column name="MSG_TIME" length="23" not-null="true" />
</property>
<property name="memberId" type="int">
<column name="MEMBER_ID" not-null="true" />
</property>
<property name="replyCount" type="int">
<column name="REPLY_COUNT" not-null="true" />
</property>
<property name="likeCount" type="int">
<column name="LIKE_COUNT" not-null="true" />
</property>
<property name="fuckCount" type="int">
<column name="FUCK_COUNT" not-null="true" />
</property>
<property name="msgText" type="string">
<column name="MSG_TEXT" not-null="true" />
</property>
<property name="msgTitle" type="string">
<column name="MSG_TITLE" not-null="true" />
</property>
</class>

and

<class name="coffeeBean.MsgTopic" table="MSG_TOPIC">
<id name="msgTopicId" type="int">
<column name="MSG_TOPIC_ID" />
<generator class="native" />
</id>
<property name="topicName" type="string">
<column name="TOPIC_NAME" not-null="true" />
</property>
</class>


reply to postreply to post
» JWorld@TW »  Object Relational Mapping » Hibernate

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8