我正在開發基於Spring框架的簡單Web應用程序。我有這兩個文件,這是映射數據庫表的迴應:抽象類和Spring問題
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;
}
}
的問題是:當我使用從通用類(吸氣)方法,爲什麼我總是得到null,當我使用來自Link類的getters時,我得到正確的數據?數據庫訪問沒問題,junit測試傳遞沒有錯誤。感謝幫助。
這與Spring和Spring-MVC無關。這些類是JPA實體。 –