2011-11-06 60 views
0

我有一個簡單的Web應用程序,它可以從數據庫表中檢索所有的輸入,並在單擊時顯示來自其他表的更多詳細信息。我正在使用休眠來映射表,但我有問題顯示來自外鍵的值。例如我要顯示與電影,而不是「[email protected]使用休眠在網頁中顯示數據庫中的值

<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> 

我目前的輸出繼電器是: [[email protected][email protected][email protected][email protected]]

但我想知道如何顯示演員的名字,而不是演員的索引?

任何幫助將是巨大的,感謝

回答

0

演員是你的情況的集合(集)。您應該迭代拋出actor併爲集合中的每個actor顯示名稱。

"[<className>@<hashCode1>, <className>@<hashCode2>, ...]" - 這是一個字符串,其方法AbstractCollection.toString()返回(電影演員設置爲AbstractCollection)。

反過來"<className>@<hashCode>" - 是一個字符串,其方法Object.toString()返回(類movie.Actor的對象)。

您可以覆蓋方法movie.Actor#toString()並使其返回演員姓名。然後你的頁面將呈現這樣的字符串:"[actor name1, actor name2, ...]"

但正確的方法是使用JSF組件,如<h:dataTable .../>或通過使用JSTL <c:forEach .../>設置的actor進行迭代。

+0

感謝您的回覆,我明白我正在使用一個集合,但我希望我不必遍歷它們。目前爲我的代碼 在我的瀏覽器輸出是:\t [電影。演員@ 7b7bee,[email protected],[email protected],[email protected]]但我希望顯示的名稱,而不是索引代碼,任何sugguestions? – shane

+0

相應地更改您的文章中的代碼plz – svaor

+0

我已更新我的答案 – svaor