-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Daten aus java.util.Set auslesen?
PostPosted: Mon Feb 04, 2008 6:37 pm 
Newbie

Joined: Sat Jan 26, 2008 7:19 am
Posts: 11
Location: Germany, Huerth
Ich bin nun so weit, dass ich meine Daten aus der Datenbank auslesen kann, dann bekomme ich ein java.util.Set, jedoch kann ich keine wirklich hilfreichen Informationen finden, wie ich die Daten dann auslesen kann. Ich habe bei Google etwas gesucht und hier ein Beispiel gefunden, wie ich Daten aus einem Set auslese, jedoch bekomme ich nur sowas wie:

Code:
de.flabben.table.Comment@1a06e38


Hier mal ein die Codestücke:

article.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="de.flabben.table.Article" schema="flabben" table="article">
        <id name="id" column="article_id" type="java.lang.Integer">
            <generator class="sequence">
                <param name="sequence">flabben.article_id_seq</param>
            </generator>
        </id>
        /* ... */
        <set name="comments" schema="flabben" table="comment">
            <key column="comment_id" />
            <one-to-many class="de.flabben.table.Comment" />
        </set>
    </class>
</hibernate-mapping>


comment.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="de.flabben.table.Comment" schema="flabben" table="comment">
        <id name="id" column="comment_id" type="java.lang.Integer">
            <generator class="sequence">
                <param name="sequence">flabben.comment_comment_id_seq</param>
            </generator>
        </id>
        <property name="title" column="comment_title" not-null="true" type="java.lang.String" />
        <property name="content" column="comment_content" not-null="true" type="java.lang.String" />
        <property name="date" column="comment_date" not-null="true" type="timestamp" />
        <property name="articleId" column="article_id" not-null="true" type="java.lang.Integer" />
        <property name="authorId" column="author_id" not-null="true" type="java.lang.Integer" />
        <one-to-one name="article" class="de.flabben.table.Article" />
        <one-to-one name="author" class="de.flabben.table.Author" />
    </class>
</hibernate-mapping>


Teil aus Example.java
Code:
Article article = (Article) session.load(Article.class, 1);
Set comments = article.getComments();

Iterator it = comments.iterator();

while (it.hasNext()) {
    System.out.println(it.next());
}

tx.commit();


Kann mir einer erklären, wie genau ich die Daten auslesen und auch verarbeiten kann? Der Artikel mit der ID 1 hat aktuell genau einen Kommentar.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 12:57 pm 
Newbie

Joined: Tue May 29, 2007 2:50 am
Posts: 12
Hallo

"de.flabben.table.Comment@1a06e38" sieht ganz nach einem Objekt der Klasse Comment aus. Die Frage ist natürlich was du erwartest, wenn du ein Objekt in der Konsole als Text ausgibst. Sinnvoller ist es eine Instanzvariable auszugeben:

Quote:
Article article = (Article) session.load(Article.class, 1);
Set comments = article.getComments();

Iterator it = comments.iterator();

while (it.hasNext()) {
Comment comment = (Comment) it.next();
System.out.println(comment.getTitle());
}

tx.commit();


Gruss
poffi


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 7:32 pm 
Newbie

Joined: Sat Jan 26, 2008 7:19 am
Posts: 11
Location: Germany, Huerth
Hm interessant, dann werde ich das die Tage direkt mal ausprobieren, danke ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.