抽象类和Spring的问题

时间:2011-07-06 07:44:10

标签: java jpa abstract-class

我正在开发一个基于Spring框架的简单webapp。我有这两个文件,它们是映射数据库表的响应:

import javax.persistence.*;
import java.util.Date;

/**
 * Abstract class for all publications of BIS
 * @author Tomek Zaremba
 */

public abstract class GenericPost {

@Temporal(TemporalType.DATE)
@Column(name = "date_added")
private Date dateAdded;

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

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

/**
 *
 * @return title of publication
 */
public String getTitle() {
    return title;
}

/**
 *
 * @param title to be set for publication
 */
public void setTitle(String title) {
    this.title = title;
}

/**
 *
 * @return description of publication
 */
public String getDescription() {
    return description;
}

/**
 *
 * @param description of publication
 */
public void setDescription(String description) {
    this.description = description;
}

/**
 *
 * @return date when publication was added
 */
public Date getDateAdded() {
    return dateAdded;
}

/**
 *
 * @param dateAdded set the date of adding for publication
 */
public void setDateAdded(Date dateAdded) {
    this.dateAdded = dateAdded;
}
}

和另一个:

import javax.persistence.*;
import java.io.Serializable;

/**
 * Instances of Link represents and collects data about single link stored in BIS    database
 * @author Tomek Zaremba
 */
@Entity
@Table(name="Link", schema="public")
public class Link extends GenericPost implements Serializable{

@Id
@Column(name="id", unique=true)
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="link_id_seq")
@SequenceGenerator(sequenceName="link_id_seq", name="link_id_seq")
private Integer id;

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_user_id")
private User user;

/**
 *
 * @return id of link
 */
public Integer getId() {
    return id;
}

/**
 *
 * @param id to be set to link
 */
public void setId(Integer id) {
    this.id = id;
}

/**
 *
 * @return link which is connected with Link instance
 */
public String getLink() {
    return link;
}

/**
 *
 * @param link to be set fo Link instance
 */
public void setLink(String link) {
    this.link = link;
}

/**
 *
 * @return User instance connected with Link instance
 */
public User getUser() {
    return user;
}

/**
 *
 * @param user to be set for Link instance
 */
public void setUser(User user) {
    this.user = user;
}
}

问题是:为什么当我使用泛型类(getters)中的方法时,我总是得到null,当我使用Link类的getter时,我得到了正确的数据?数据库访问很好,junit测试正在通过而没有错误。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

GenericPost类应使用@MappedSuperclass进行注释。否则,映射不会考虑其持久性注释。

注意:您的单元测试应该更新,以检查超类字段的映射是否按预期工作。

答案 1 :(得分:0)

我认为这不是Spring的错。您需要使用@MappedSuperclass

为您的父类添加注释
相关问题