繁体   English   中英

如何将实体对象绑定到本地@Transient属性?

[英]How do I can bind an entity object to a local @Transient property?

EmployeeInfo实体类

@Entity
@Table(name = "employeeinfo")
public class EmployeeInfo{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "employeeId")
private String employeeId;
@Column(name = "firstName")
private String firstName;
@Column(name = "middleName")
private String middleName;
@Column(name = "lastName")
private String lastName;

......

}

另一个Entity类ProjectTaskComments

@Entity
@Table(name = "projecttaskcomments")
public class ProjectTaskComments{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @Column(name = "comments")
    private String comments;

    @Basic(optional = false)
    @Column(name = "commentTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date commentTime;
    @Column(name = "fkCommentedBy")
    private Long fkCommentedBy;


    @Transient
    @JsonIgnoreProperties
    private EmployeeInfo commentedEmployee;
    @Transient
    @Autowired
    EmployeeInfoService employeeInfoService; 


   public EmployeeInfo getCommentedEmployee() {
        EmployeeInfo employeeInfo;
        employeeInfo = employeeInfoService.getSingleEmployeeInfoByFkUserId(this.fkCommentedBy);
        if(employeeInfo != null) {
            this.commentedEmployee.setEmployeeId(employeeInfo.getEmployeeId());
            this.commentedEmployee.setFirstName(employeeInfo.getFirstName());
            this.commentedEmployee.setMiddleName(employeeInfo.getMiddleName());
            this.commentedEmployee.setLastName(employeeInfo.getLastName());
            this.commentedEmployee.setPhoto(employeeInfo.getPhoto());
            return commentedEmployee;
        } else {
            return null;
        }
    }

}

我试图通过fkCommentedBy属性在getCommentedEmployee()方法中找到一个EmployeeInfo对象,并设置为@Transient属性commentedEmployee。

我发现以下错误:

2018-10-11 13:07:56.834  WARN 16756 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[1]->com.activeboss.model.pm.ProjectTasks["projecttaskcommentsCollection"]->org.hibernate.collection.internal.PersistentBag[0]->com.activeboss.model.pm.ProjectTaskComments["commentedEmployee"])
2018-10-11 13:07:56.853  WARN 16756 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[1]->com.activeboss.model.pm.ProjectTasks["projecttaskcommentsCollection"]->org.hibernate.collection.internal.PersistentBag[0]->com.activeboss.model.pm.ProjectTaskComments["commentedEmployee"])

我该如何解决?

@Transient的目的是为非持久属性建模,因此我不清楚为什么要在通过“ fkCommentedBy”属性持久化commentedByEmployee属性时对@transient进行建模。 IMO,@ ManyToOne在这种情况下更合适。

@Entity
@Table(name = "projecttaskcomments")
public class ProjectTaskComments {

// .... other declarations 
@ManyToOne
@JoinColumn(name="fkCommentedBy")
private EmployeeInfo commentedEmployee;
// ..... other code
}

现在,如果仍然要使用@Transient,则需要在getter方法中确保对EmployeeInfoService对象具有有效的引用。 @Autowired在这里不起作用,因为ProjectTaskComments不是spring托管的bean。

同意@KishoreKirdat,需要检查null并进行一些初始化:

public EmployeeInfo getCommentedEmployee() {
  // check here
  if (employeeInfoService == null) return null;

  EmployeeInfo employeeInfo = employeeInfoService.getSingle...;
  if (employeeInfo != null) {
    // init here
    commentedEmployee = new EmployeeInfo();

    commentedEmployee.set...;
    return commentedEmployee;
  } else {
    return null;
  }
}

private void setCommentedEmployee(EmployeeInfo employeeInfo) {
  // do nothing
}

是的,终于可以解决了。 我刚刚做了以下工作:

  1. 在ProjectTaskComments类中添加了@Component:

     @Entity @Component @Table(name = "projecttaskcomments") public class ProjectTaskComments{ ........ 
  2. 将EmployeeInfoService声明为静态,并为该服务添加了一个seter方法,并对其进行@Autowired。

     @Transient private static EmployeeInfoService employeeInfoService; @Autowired public void setEmployeeInfoService(EmployeeInfoService employeeInfoService) { this.employeeInfoService = employeeInfoService; } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM