gpt4 book ai didi

java - 抽象类和Spring的问题

转载 作者:行者123 更新时间:2023-11-29 09:20:50 29 4
gpt4 key购买 nike

我正在开发一个基于 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;
}
}

问题是:为什么当我使用泛型类(getters)的方法时,我总是得到 null 而当我使用 Link 类的 getters 时,我得到正确的数据?数据库访问正常,junit 测试顺利通过。感谢您的帮助。

最佳答案

GenericPost 类应该用 @MappedSuperclass 注释.否则,映射不考虑其持久性注释。

注意:您的单元测试可能应该更新以检查父类(super class)字段的映射是否按预期工作。

关于java - 抽象类和Spring的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6593097/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com