I'm developing a simple webapp based on Spring framework. I have these two files which are response for mapping database tables:
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;
}
}
and another:
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;
}
}
The problem is: why when I'm using methods from generic class (getters) I always get null and when I use getters from Link class I get correct data? Database access is fine, the junit tests are passing without errors. Thanks for help.
The GenericPost
class should be annotated with @MappedSuperclass
. Else, its persistence annotations are not taken into account by the mapping.
Note : Your unit tests should probably be updated to check that the mapping of the super-class fields works as expected.
I don't think that's Spring's fault. You need to annotate your parent class with @MappedSuperclass
精彩评论