在Hibernate中获取插入的id是一种更好的方法吗?

时间:2013-03-08 05:18:20

标签: java hibernate struts2 ognl actionresult

我正在使用Hibernate 3.0和Struts。我正在使用@GeneratedValue来生成id。

当我保存客户详细信息时,我有一个客户屏幕我想要移动到下一个屏幕 新插入customerId因为只根据该ID我将保存第二页详细信息

因为我正在使用

someDAO.save(customer);
customerId = customer.getCustomerId();

我在保存并使用Struts 2后获得客户对象值redirectAction我将customerId转发到第二页,它工作正常但我在控制台上收到警告

Caught OgnlException while setting property 'applicationId' on type 'org.apache.struts2.dispatcher.ServletActionRedirectResult'.
ognl.NoSuchPropertyException: org.apache.struts2.dispatcher.ServletActionRedirectResult.applicationId
    at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:132)
    at com.opensymphony.xwork2.util.OgnlValueStack$ObjectAccessor.setProperty(OgnlValueStack.java:68)
    at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
    at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
    at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
    at ognl.SimpleNode.setValue(SimpleNode.java:246)

2 个答案:

答案 0 :(得分:2)

最好的方法你可以忽略这个,

然而 redirectAction 使用 ActionMapperFactory 提供的 ActionMapper 将浏览器重定向到调用指定操作的URL,并且(可选)<强>命名空间即可。 OGNL尝试使用源页面上指定的值设置map html元素,并将这些值推送到 valuestack 中。有时 OGNL 会尝试使用设置者映射值,这些设置符在操作类中使用source -page指定,但未能设置值,导致警告

您可以尝试明确指定参数来禁止警告:

<result name="showReportResult" type="redirectAction">
     <param name="applicationId">${applicationId}</param>
 </result>

答案 1 :(得分:1)

例外说

  

如果尝试从某个属性中提取属性,则抛出异常   没有这种属性的对象

这意味着您需要Java bean属性applicationId。在场上放置getter和setter应该可以解决问题。

关于使用hibernate使用

插入id的更好方法
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id", nullable = false)
public Long getId() {
    return id;
}