简体   繁体   English

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

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

EmployeeInfo entity class 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;

......

} }

Another Entity class ProjectTaskComments 另一个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;
        }
    }

}

I tried to find an EmployeeInfo object in getCommentedEmployee() method by fkCommentedBy property and set to @Transient property commentedEmployee. 我试图通过fkCommentedBy属性在getCommentedEmployee()方法中找到一个EmployeeInfo对象,并设置为@Transient属性commentedEmployee。

I found the following errors: 我发现以下错误:

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"])

How do I solve it? 我该如何解决?

The purpose of @Transient is to model a non persistent attribute, so it is unclear to me why do you want to @Transient on commentedByEmployee attribute when you have it persisted via "fkCommentedBy" attribute. @Transient的目的是为非持久属性建模,因此我不清楚为什么要在通过“ fkCommentedBy”属性持久化commentedByEmployee属性时对@transient进行建模。 IMO, @ManyToOne is more appropriate in this case. IMO,@ ManyToOne在这种情况下更合适。

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

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

Now if you still want to use @Transient, then in the getter method you need to make sure that you have a valid referent to EmployeeInfoService object. 现在,如果仍然要使用@Transient,则需要在getter方法中确保对EmployeeInfoService对象具有有效的引用。 @Autowired will not work here as the ProjectTaskComments is not a spring managed bean. @Autowired在这里不起作用,因为ProjectTaskComments不是spring托管的bean。

Agree @KishoreKirdat, neet to check null and do some initialization: 同意@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
}

Yes, finally I could solve it. 是的,终于可以解决了。 I just did the following works: 我刚刚做了以下工作:

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

     @Entity @Component @Table(name = "projecttaskcomments") public class ProjectTaskComments{ ........ 
  2. Declared EmployeeInfoService as static and added a seter method for the service and @Autowired it. 将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.

相关问题 如何将Spring Data Projection与Transient属性一起使用? - How can I use Spring Data Projection with a Transient property? 如何解决对象引用未保存的瞬态实例的错误-在刷新之前保存瞬态实例? - How do I solve this error of object references an unsaved transient instance - save the transient instance before flushing? 如何在大型项目上修复“对象引用未保存的瞬态实例-在刷新之前保存瞬态实例”? - How can I fix “object references an unsaved transient instance - save the transient instance before flushing” on a large project? 如何在本机SQL查询中绑定实体对象中的@Transient字段 - How can binding @Transient field in entity object from native SQL query Hibernate使用临时对象更新实体 - Hibernate update an entity with a transient object 怎么说冬眠我真的有短暂的实体? - How to say to hibernate that I really have transient entity? 如何绑定组合对象的属性 - How to bind a property of a composing object JavaFX:如何将Button属性绑定到线程状态? - JavaFX: How do I bind a Button property to a thread status? 如何在Java中将实体转换为对象 - How can I convert Entity to Object in java 如何在 spring JPA 中设置用 @Transient 注释的变量形成非瞬态变量的设置器? - How can I set variable annotated with @Transient form a setter of non transient variable in spring JPA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM