使用hibernate在网页中显示db的值

时间:2011-11-06 17:57:40

标签: java mysql hibernate web

我有一个简单的Web应用程序,可以从db表中检索所有enterys,单击时可以显示其他表中的更多详细信息。我正在使用hibernate映射表,但我在显示外键值时遇到问题。例如,我想显示与电影相关联的演员姓名而不是“movie.Actor@5514f9”

 <h:outputText value="Actors"/>
    <h:outputText value="#{movieController.selected.actors}" title="Actors"/>

我使用的代码是否显示值。

我的movieController类是

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package movie;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;


/**
 *
 * @author Shane
 */
@ManagedBean(name = "movieController")
@SessionScoped
public class MovieController {

int startId;
int endId;
DataModel movieTitles;
MovieHelper helper;
//private int recordCount = 1000;
private int pageSize = 10;

private Movie current;
private int selectedItemIndex;

public List getActors() {
    List actors = new ArrayList();

    return actors;
}

/** Creates a new instance of MovieController */
public MovieController() {
    helper = new MovieHelper();
    startId = 1;
    endId = 1000000;
}

public MovieController(int startId, int endId) {
    helper = new MovieHelper();
    this.startId = startId;
    this.endId = endId;
}

public Movie getSelected() {
    if (current == null) {
        current = new Movie();
        selectedItemIndex = -1;
    }
    return current;
}

public DataModel getMovieTitles() {
    if (movieTitles == null) {
        movieTitles = new ListDataModel(helper.getMovieTitles(startId, endId));
    }
    return movieTitles;
}

void recreateModel() {
    movieTitles = null;
}

//  The following methods that are used for page navigation
    public boolean isHasNextPage() {
    helper = new MovieHelper();

    if (endId + pageSize <= helper.getRecordCount()) {
        return true;
    }
    return false;
}

public boolean isHasPreviousPage() {
    if (startId - pageSize > 0) {
        return true;
    }
    return false;
}

public String next() {
    startId = endId + 1;
    endId = endId + pageSize;
    recreateModel();
    return "index";
}

public String previous() {
    startId = startId - pageSize;
    endId = endId - pageSize;
    recreateModel();
    return "index";
}

public int getPageSize() {
    return pageSize;
}

public String prepareView() {
    current = (Movie) getMovieTitles().getRowData();
    return "browse";
}

public String prepareList() {
    recreateModel();
    return "index";
    }
}

我的Movie.hbm.xml文件是

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 26-Oct-2011 22:41:19 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="movie.Movie" table="movie" catalog="movieserver">
    <id name="id" type="int">
        <column name="Id" />
        <generator class="assigned" />
    </id>
    <property name="title" type="string">
        <column name="Title" length="75" />
    </property>
    <property name="rating" type="string">
        <column name="Rating" length="25" />
    </property>
    <property name="plot" type="string">
        <column name="Plot" />
    </property>
    <property name="poster" type="string">
        <column name="Poster" length="150" />
    </property>
    <property name="runtime" type="string">
        <column name="Runtime" length="15" />
    </property>
    <property name="votes" type="string">
        <column name="Votes" length="15" />
    </property>
    <property name="released" type="string">
        <column name="Released" length="15" />
    </property>
    <property name="year" type="string">
        <column name="Year" length="15" />
    </property>
    <property name="rated" type="string">
        <column name="Rated" length="15" />
    </property>
    <property name="director" type="string">
        <column name="Director" length="100" />
    </property>
    <set name="genres" inverse="false" table="movie_genre">
        <key>
            <column name="MovieId" not-null="true" />
        </key>
        <many-to-many entity-name="movie.Genre">
            <column name="GenreId" not-null="true" />
        </many-to-many>
    </set>
    <set name="writers" inverse="false" table="movie_writer">
        <key>
            <column name="MovieId" not-null="true" />
        </key>
        <many-to-many entity-name="movie.Writer">
            <column name="WriterId" not-null="true" />
        </many-to-many>
    </set>
    <set name="actors" inverse="false" table="movie_actor">
        <key>
            <column name="MovieId" not-null="true" />
        </key>
        <many-to-many entity-name="movie.Actor">
            <column name="ActorId" not-null="true" />
        </many-to-many>
        </set>
    </class>
</hibernate-mapping>

我目前的输出是: [movie.Actor@7b7bee,movie.Actor@10580fa,movie.Actor@3c6b88,movie.Actor@56a1b6]

但我想知道如何显示演员姓名而不是演员的索引?

对此的任何帮助都会很棒,谢谢

1 个答案:

答案 0 :(得分:0)

Actors是你的案例中的集合(集合)。你应该迭代throw actor,并为集合中的每个actor显示名称。

"[<className>@<hashCode1>, <className>@<hashCode2>, ...]" - 这是一个方法AbstractCollection.toString()返回的字符串(电影演员设置为AbstractCollection)。

反过来"<className>@<hashCode>" - 是一个方法Object.toString()返回的字符串(类movie.Actor的对象)。

您可以覆盖方法movie.Actor#toString()并使其返回actor名称。然后您的页面将呈现如下字符串:"[actor name1, actor name2, ...]"

但正确的方法是使用像<h:dataTable .../>这样的JSF组件,或者使用JSTL <c:forEach .../>迭代actor集。