非法参数异常:发生了调用id的getter

时间:2015-09-01 05:29:25

标签: java hibernate

我正在尝试在数据库中插入记录。这是第一次正确插入。但是,如果我第二次插入带有不同Id的更新字段详细信息的记录,则会抛出

  

数据访问异常:非法参数异常:发生调用   id的getter

我无法找出原因。

这是堆栈跟踪: -

com.abc.dac.exception.DataAccessException: org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred calling getter of com.abc.dac.domain.ActivityLog.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.abc.dac.domain.ActivityLog.id
        at com.abc.dac.hours.dao.impl.ActivityLogDAOImpl.insertRecord(ActivityLogDAOImpl.java:39)
        at com.abc.dac.hours.service.impl.HoursServiceImpl.insertmethod(HoursServiceImpl.java:2850)
        at com.abc.dac.hours.service.impl.HoursServiceImpl.updateReviewTrack(HoursServiceImpl.java:1186)
        at com.abc.dac.hours.web.HoursController.updateReviewStatus(HoursController.java:1741)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at com.abc.dac.AuthorizationFilter.doFilter(AuthorizationFilter.java:35)
        at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
        at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at com.abc.dac.AuthorizationFilter.doFilter(AuthorizationFilter.java:35)
        at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
        at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:563)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Unknown Source)
    Caused by: org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred calling getter of com.abc.dac.domain.ActivityLog.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.abc.dac.domain.ActivityLog.id
        at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
        at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
        at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
        at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
        at com.abc.dac.hours.dao.impl.ActivityLogDAOImpl.insertRecord(ActivityLogDAOImpl.java:33)
        ... 39 more
    Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.abc.dac.domain.ActivityLog.id
        at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
        at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
        at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3589)
        at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3305)
        at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
        at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
        at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
        at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
        at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
        at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
        at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
        at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
        at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
        at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
        at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
        at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
        at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
        at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
        ... 42 more
    Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
        ... 63 more

POJO课程是: -

@Entity(name = "activitylog")
@Table(name = "activitylog")
public class ActivityLog {

private Integer id;
private Integer employeeId;
private String firstname;
private String lastname;
private Date submissiondate = null;
private Employee manager = null;
private Date reviewdate = null;
private String activity=null;
private Short month;
private Short year;


/**
 * @return id
 */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id",unique = true, nullable = false)
public Integer getId() {
    return id;
}

/**
 * @param id
 */
public void setId(Integer id) {
    this.id = id;
}





@Column(name="employeeId", nullable=false)
public Integer getEmployeeId() {
    return employeeId;
}
public void setEmployeeId(Integer employeeId) {
    this.employeeId = employeeId;
}
@Column(name = "firstname", nullable = false, length = 45)
public String getFirstName() {
    return firstname;
}
public void setFirstName(String firstName) {
    this.firstname = firstName;
}

@Column(name = "lastname", nullable = false, length = 45)
public String getLastName() {
    return lastname;
}
public void setLastName(String lastName) {
    this.lastname = lastName;
}
@Temporal(TemporalType.DATE)
@Column(name = "submissiondate")
public Date getSubmissiondate() {
    return submissiondate;
}
public void setSubmissiondate(Date submissiondate) {
    this.submissiondate = submissiondate;
}
@ManyToOne(fetch = FetchType.EAGER, optional = true, targetEntity = ActivityLog.class)
@JoinColumn(name = "manager")
public Employee getManager() {
    return manager;
}
public void setManager(Employee manager) {
    this.manager = manager;
}
@Temporal(TemporalType.DATE)
@Column(name = "reviewdate")
public Date getReviewdate() {
    return reviewdate;
}
public void setReviewdate(Date reviewdate) {
    this.reviewdate = reviewdate;
}
@Column(name = "activity", nullable = false, length = 345)
public String getActivity() {
    return activity;
}
public void setActivity(String activity) {
    this.activity = activity;
}
@Column(name = "month", nullable = false)
public Short getMonth() {
    return month;
}
public void setMonth(Short month) {
    this.month = month;
}
@Column(name = "year", nullable = false)
public Short getYear() {
    return year;
}
public void setYear(Short year) {
    this.year = year;
}


}

insert方法调用发生在两个地方。 第一个是: -

if(activityLog ==null){
            activityLog = new ActivityLog();
            activityLog.setEmployeeId(loggedInUser.getId());
            activityLog.setFirstName(loggedInUser.getFirstName());
            activityLog.setLastName(loggedInUser.getLastName());
            activityLog.setSubmissiondate(DLCUtils.getCurrentDate()); 
            activityLog.setMonth(currentMonth);
            activityLog.setYear(currentYear);
            activityLog.setActivity("Timesheet is submitted for the month of"+" "+DLCUtils.getMonthName(currentMonth)+" "+currentYear);
            //activityLog.setManager(loggedInUser.getManager());
            //activityLog.setReviewdate(reviewdate);*/
            insertmethod(activityLog);
            //m_activityLogDao.insertRecord(activityLog);
            }

上面的插入发生得很好,因为这是第一次.Below是我第二次调用插入方法来添加另一条记录,其中一些字段已更改且具有新的Id。但它在这里失败了。

activityLog=m_activityLogDao.getRecord(employee.getId() ,new Integer(
                year).shortValue(), new Integer(month).shortValue());
        activityLog1 = new ActivityLog();
        activityLog1.setManager(employee.getManager());
        activityLog1.setReviewdate(DLCUtils.getCurrentDate());
        System.out.println("latest1");
        activityLog1.setEmployeeId(employee.getId());

        activityLog1.setFirstName(activityLog.getFirstName());
        activityLog1.setLastName(activityLog.getLastName());
        activityLog1.setSubmissiondate(activityLog.getSubmissiondate());
        activityLog1.setMonth(activityLog.getMonth());
        activityLog1.setYear(activityLog.getYear());
        activityLog1.setActivity(activityLog.getActivity());
//      activityLog1.setId(5);

        if (action.equals(StringConstants.ACCEPT_EMPLOYEE_HOURS)) {

            reviewTrack.setStatus(1);
            m_reviewTrackDao.updateRecord(reviewTrack);
            activityLog1.setActivity("Time sheet"+"for "+DLCUtils.getMonthName(month)+" "+year+ " is accepted by"+" "+employee.getManager().getFullName());
            insertmethod(activityLog1);
}
public void insertmethod(ActivityLog activitylog){
        try {
            m_activityLogDao.insertRecord(activitylog);
        } catch (DataAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

以下是insertrecord方法的实现: -

 @Override
    public void insertRecord(ActivityLog activityLog) throws DataAccessException
    {
        try
        {

            hibernateTemplate.save(activityLog);
           // hibernateTemplate.flush();
        }
        catch(Exception e)
        {
            m_logger.error("Error in inserting record in activity_Log table", e);
            throw new DataAccessException(e);
        }

    }

代码在执行hibernateTemplate.save(activityLog)语句时抛出异常。

1 个答案:

答案 0 :(得分:1)

错误是@ManyToOne(fetch = FetchType.EAGER, optional = true, targetEntity = ActivityLog.class) @JoinColumn(name = "manager") public Employee getManager() { return manager; }

的声明
targetEntity

只要Hibernate能够自行推断,您就不需要指定Employee。在这种情况下,很明显它是Employee。并且,由于您已经为而不是 Employee的子类提供了目标实体类,因此在尝试将ActivityLog强制转换为targentEntity时会抛出上述异常。< / p>

删除{{1}},你没事。