[英]Problem with abstract class and Spring
我正在開發一個基於 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;
}
}
問題是:為什么當我使用通用 class(吸氣劑)中的方法時,我總是得到 null 而當我使用來自鏈接 class 的吸氣劑時,我得到正確的數據? 數據庫訪問很好,junit 測試通過,沒有錯誤。 感謝幫助。
GenericPost
class 應該用@MappedSuperclass
注釋。 否則,映射不會考慮其持久性注釋。
注意:您的單元測試可能應該更新以檢查超類字段的映射是否按預期工作。
我不認為這是春天的錯。 您需要使用 @MappedSuperclass 注釋您的父@MappedSuperclass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.