【问题标题】:How to map Form Parameter with Hibernate Entity Class in Spring-MVC Controller [duplicate]如何在 Spring-MVC 控制器中将表单参数与 Hibernate 实体类映射 [重复]
【发布时间】:2018-01-24 00:46:18
【问题描述】:

我是 Hibernate/JPA 的新手,我正在尝试使用 hibernate 实体类获取表单参数。直到我尝试使用与其他类有关系的实体类获取参数时,它才出现问题。例如;

控制器:

@RequestMapping(value = "/addProduct", method = RequestMethod.POST)
    public String addProduct(Model model, Product product) {
        databaseService.insert(product);
        return "redirect:/products";
    }

实体类:

@Entity
@Table(name = "products")
public class Product implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    private Category category;

    @Column(name = "name")
    private String name;

    @Column(name = "price")
    private String price;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

分类类:

@Entity
@Table(name = "categories")
public class Category implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private String id;

    @Column(name = "name")
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

程序无法设置“类别”。因为类别不是 int、string 之类的类型。我意识到了这个问题。但我找不到使用实体类映射参数的解决方案。有什么办法可以解决这个问题。或者我应该使用@RequestParam 来一一获取参数,而不是使用实体类映射参数。

更新

我只是在我的 .jsp 页面中将 category 更改为 category.id,它解决了我的问题。 旧代码

<form>
...
    <select class="form-control" name="category">
    <c:if test="${not empty categoryList}">
        <c:forEach var="item" items="${categoryList}">
            <option value="${item.getId()}">${item.getName()}</option>
        </c:forEach>
    </c:if>
    </select>
</form>

新代码

<form>
...
    <select class="form-control" name="category.id">
    <c:if test="${not empty categoryList}">
        <c:forEach var="item" items="${categoryList}">
            <option value="${item.getId()}">${item.getName()}</option>
        </c:forEach>
    </c:if>
    </select>
</form>

【问题讨论】:

  • 您还需要在 Category 类中的字段上添加注释。你能把Category chas的内容贴在这里吗?
  • 我更新了帖子。可以看到分类内容。
  • 在 post 方法中,您可以从模型中检索反序列化的任何参数。
  • 您没有从 Category 到实体类的反向映射(One 到 Man7)。您可能还需要添加反向属性。

标签: java spring hibernate spring-mvc jpa


【解决方案1】:

请向我们展示您的表单映射,
在那之前可以尝试,将&lt;form:select&gt;/&lt;form:input&gt;标签中的path更改为category.idcategory.name

看看我的另一个answer

我建议不要在View中暴露你的Entity,尝试在DTO中获取表单数据,然后转换为实体。 .

【讨论】:

  • 我认为你是对的。我将使用 DTO。经过一番搜索,人们建议使用 DTO 映射表单对象。甚至人们强烈不建议使用实体类来映射表单参数。
  • 我重新设计了我的代码。所以我现在不能尝试。但我会。而且我不能投票,因为我的声誉不到 15 岁。
  • 是的,它是工作。感谢您的回答。我标记了接受答案。
【解决方案2】:

一种方法是创建自定义 Spring 转换器。因此,假设您将实体的 Id 作为路径变量传递,并且您的转换器实现将为您获取该产品对象。

在您的 Controller 中,您需要执行以下操作:

 @RequestMapping(value = "/addProduct/{id}", method = RequestMethod.POST)
    public String addProduct(Model model, @PathVariable("id") Product product) {
        databaseService.insert(product);
        return "redirect:/products";
    }

您的转换器将如下所示:

    import org.springframework.core.convert.converter.Converter;
    public class StringToProductConverter implements Converter<String, Product> {
    ...
    @Override
        public Product convert(String id) {
            Product product = databaseService.getProduct(id);
            ...
            return product;
        }

并且不要忘记根据您正在使用的 Spring 版本以编程方式或通过 XML 注册您的转换器。

【讨论】:

    猜你喜欢
    • 2015-05-09
    • 2018-03-27
    • 2014-12-29
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多