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?