Tomasz Faszyński

Tomasz Faszyński Specjalista ds.
programowania

Temat: Hibernate + adnotacje + relacje

Mam taki kod:


@Entity(name="News")
@Table(name="aplikacja_news")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class News {

@Id
@Column(name="id")
@SequenceGenerator(name = "id_seq", sequenceName = "id")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "id_seq")
private int id;

//@NotNull
@Column(name="id_author")
private Integer id_author;

//@NotEmpty
@Column(name="content")
private String content;

//@NotNull
@Column(name="topic")
private String topic;

private Authors author;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id", nullable = false)
public Authors getAuthor() {
return author;
}

public void setAuthor(Authors author) {
this.author = author;
}

public News() {
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Integer getId_author() {
return id_author;
}

public void setId_author(Integer id_author) {
this.id_author = id_author;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getTopic() {
return topic;
}

public void setTopic(String topic) {
this.topic = topic;
}
}



@Entity(name="Authors")
@Table(name="aplikacja_authors")
public class Authors {

@Id
@Column(name="id_author")
@SequenceGenerator(name = "id_seq", sequenceName = "id")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "id_seq")
private int id_author;

@Column(name="name")
private String name;

@Column(name="surname")
private String surname;

private Set<News> news;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "author")
@JoinColumn(name = "id_author")
public Set<News> getNews() {
return news;
}

public void setNews(Set<News> news) {
this.news = news;
}

public int getId_author() {
return id_author;
}

public void setId_author(int id_author) {
this.id_author = id_author;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}
}


Łączenie dwóch tabel po author_id. Otrzymuję jednak błąd:


Caused by: org.hibernate.MappingException: Could not determine type for: com.code.domain.Authors, at table: cgi_news, for columns: [org.hibernate.mapping.Column(author)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)


Z czego to wynika? Nie konfigurowałem, żadnych xmli. Powinienem?

Bo jeśli dodałem w aplikacja-portlet.xml:

<bean id="hibernateSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="hibernateDataSource" />


<property name="schemaUpdate" value="true" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>

<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.code.domain.News</value>
<value>com.code.domain.Authors</value>
</list>
</property>

<mapping class="com.code.domain.News" />
<mapping class="com.code.domain.Authors" />
</bean>


To przy <mapping> otrzymuję błąd:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'mapping'. One of '{"http://www.springframework.org/schema/
beans":meta, "http://www.springframework.org/schema/beans":constructor-arg, "http://www.springframework.org/schema/beans":property,
"http://www.springframework.org/schema/beans":qualifier, "http://www.springframework.org/schema/beans":lookup-method, "http://
www.springframework.org/schema/beans":replaced-method, WC[##other:"http://www.springframework.org/schema/beans"]}' is expected.


W czym jest problem?

konto usunięte

Temat: Hibernate + adnotacje + relacje

Poczytaj blizniaczy watek o Eclipse + JPA.
Z klasy News wywal

//@NotNull
@Column(name="id_author")
private Integer id_author;

public Integer getId_author() {
return id_author;
}

public void setId_author(Integer id_author) {
this.id_author = id_author;
}


Te adnotacje mozesz polozyc natomiast tu (ale masz je juz na getterze):

@NotNull
@Column(name="id_author")//Tu podajesz nazwe kolumny "w tej drugiej" tabeli
private Authors author;

Generalnie staraj sie trzymac jednej konwencji: albo adnotacje na polach albo na getterach.

Klasa Authors:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "author")
//@JoinColumn(name = "id_author")
//Tutaj nie pamietam, ale przy OneToMany raczej nie podaje sie joinColumn,
//Zwlaszcza jesli zdefiniowales "z drugiej strony" - doczytaj
public Set<News> getNews() {
return news;
}
Wlodzimierz M. edytował(a) ten post dnia 06.09.12 o godzinie 08:25
Tomasz Faszyński

Tomasz Faszyński Specjalista ds.
programowania

Temat: Hibernate + adnotacje + relacje

Wlodzimierz M.:

Te adnotacje mozesz polozyc natomiast tu (ale masz je juz na getterze):

@NotNull
@Column(name="id_author")//Tu podajesz nazwe kolumny "w tej drugiej" tabeli
private Authors author;

Czyli w tabeli News?
Tomasz Faszyński

Tomasz Faszyński Specjalista ds.
programowania

Temat: Hibernate + adnotacje + relacje

A jak teraz w widoku jsp wyświetlić w oprócz informacji o news również imię autora?

Mam coś takiego:


<table border="1" width="500px">
<c:forEach var="news" items="${wiadomosci}">
<tr>
<td valign="top" width="50px"><c:out value="${news.author.name}" /></td>
<td valign="top" width="120px"><c:out value="${news.topic}" /></td>
<td valign="top" width="300px"><c:out value="${news.content}" /></td>
<td valign="top" width="20px">
<a href=
<portlet:actionURL>
<portlet:param name='myaction' value='removeNews' />
<portlet:param name='id' value="${news.id}" />
</portlet:actionURL> ><b>Remove</b></a></td>
</tr>
</c:forEach>

<c:out value="${helloWorldMessage}"/>
</table>


Jak w pole ${news.author.name} wpisać imię autora?

Bo teraz mam błąd:


Caused by: java.lang.NumberFormatException: For input string: "author"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
at org.apache.jsp.WEB_002dINF.jsp.helloWorld_jsp._jspx_meth_c_005fout_005f0(helloWorld_jsp.java:239)
at org.apache.jsp.WEB_002dINF.jsp.helloWorld_jsp._jspx_meth_c_005fforEach_005f0(helloWorld_jsp.java:189)
at org.apache.jsp.WEB_002dINF.jsp.helloWorld_jsp._jspService(helloWorld_jsp.java:117)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
... 202 more
10:06:37,860 ERROR [DispatcherPortlet:559] Could not complete request
javax.portlet.PortletException: org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/helloWorld.jsp at line 14

11: <table border="1" width="500px">
12: <c:forEach var="news" items="${wiadomosci}">
13: <tr>
14: <td valign="top" width="50px"><c:out value="${news.author.name}" /></td>
15: <td valign="top" width="120px"><c:out value="${news.topic}" /></td>
16: <td valign="top" width="300px"><c:out value="${news.content}" /></td>
17:

Następna dyskusja:

Java/J2EE/Spring/Swing/Hibe...




Wyślij zaproszenie do