简体   繁体   English

无法将“java.lang.String”类型的属性值转换为属性“transactions”所需的类型“java.util.List”

[英]Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'transactions'

I am working with thymeleaf and getting some errors regarding conversion of data from String to List.我正在使用 thymeleaf 并在将数据从字符串转换为列表时遇到一些错误。 Here I have attached my code在这里我附上了我的代码

My entity classes:我的实体类:

@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "first_name")
@NotNull(message = "First Name cannot be empty")
@Size(min = 1, message = "First Name cannot be empty")
private String firstName;

@Column(name = "last_name")
@NotNull(message = "Last Name cannot be empty")
@Size(min = 1, message = "Last Name cannot be empty")
private String lastName;

@Column(name = "email")
@NotNull(message = "Email ID cannot be empty")
@Pattern(regexp = "^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9.-]+$",
        message = "Enter valid mail id")
private String email;

@Column(name = "branch")
@NotNull(message = "Branch name cannot be empty")
private String branch;

@Column(name = "balance")
private double balance;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
private List<Transaction> transactions;

public Customer(String firstName, String lastName, String email, String branch, double balance) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.branch = branch;
    this.balance = balance;
}

public Customer() {
}

public int getId() {
    return id;
}

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

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getBranch() {
    return branch;
}

public void setBranch(String branch) {
    this.branch = branch;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public List<Transaction> getTransactions() {
    return transactions;
}

public void setTransactions(List<Transaction> transactions) {
    this.transactions = transactions;
}

public void addTransaction(Transaction transaction){
    if(transaction == null) {
        transactions = new ArrayList<>();
    }
    transactions.add(transaction);
}

@Override
public String toString() {
    return "Customer{" +
            "id=" + id +
            ", firstName='" + firstName + '\'' +
            ", lastName='" + lastName + '\'' +
            ", email='" + email + '\'' +
            ", branch='" + branch + '\'' +
            ", balance=" + balance +
            '}';
  }


@Entity
@Table(name = "transactions")
public class Transaction {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "transaction")
private double amount;

public Transaction() {
}

public Transaction(double amount) {
    this.amount = amount;
}

public int getId() {
    return id;
}

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

public double getAmount() {
    return amount;
}

public void setAmount(double amount) {
    this.amount = amount;
}

@Override
public String toString() {
    return "Transaction{" +
            "id=" + id +
            ", amount=" + amount +
            '}';
}

Html Form: html格式:

    <h3>Customer Directory</h3>
    <hr>

    <p class="h4 mb-4">Save Customer</p>

    <form action="#" th:action="@{/customers/saveCustomer}"
          th:object="${customer}" method="POST">

        <!-- Add hidden form field to handle the update -->
        <input type="hidden" th:field="*{id}" />

        <input type="text" th:field="*{firstName}"
            class="form-control mb-4 col-4" placeholder="First Name">

        <input type="text" th:field="*{lastName}"
               class="form-control mb-4 col-4" placeholder="Last Name">

        <input type="text" th:field="*{email}"
               class="form-control mb-4 col-4" placeholder="Email">

        <input type="text" th:field="*{branch}"
               class="form-control mb-4 col-4" placeholder="Branch">

        <input type="text" th:field="*{balance}"
               class="form-control mb-4 col-4" placeholder="Balance">

        <input type="text" th:field="*{transactions}"
               class="form-control mb-4 col-4" placeholder="Transactions">

        <!--input type="hidden" th:field="*{transactions}" /-->

        <button type="submit" class="btn btn-info col-2">Save</button>

    </form>

    <hr>
    <a th:href="@{/customers/list}">Back to Customers List</a>

</div>

Error:错误:

Validation failed for object='customer'. object='customer' 验证失败。 Error count: 1 org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'customer' on field 'transactions': rejected value [[Transaction{id=1, amount=1000.0}, Transaction{id=3, amount=100.0}]];错误计数:1 org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1 个错误字段“交易”对象“客户”中的字段错误:拒绝值 [[Transaction{id=1, amount=1000.0},交易{id=3,金额=100.0}]]; codes [typeMismatch.customer.transactions,typeMismatch.transactions,typeMismatch.java.util.List,typeMismatch];代码 [typeMismatch.customer.transactions,typeMismatch.transactions,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customer.transactions,transactions];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [customer.transactions,transactions]; arguments [];参数 []; default message [transactions]];默认消息[交易]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'transactions';默认消息 [无法将“java.lang.String”类型的属性值转换为属性“transactions”所需的类型“java.util.List”; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.OneToMany @javax.persistence.JoinColumn com.kaneki.springboot.bankapplication.entity.Transaction] for value '[Transaction{id=1, amount=1000.0}, Transaction{id=3, amount=100.0}]';嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@javax.persistence.OneToMany @javax.persistence.JoinColumn com.kaneki.springboot.bankapplication.entity。交易] 为值 '[Transaction{id=1, amount=1000.0}, Transaction{id=3, amount=100.0}]'; nested exception is java.lang.NumberFormatException: For input string: "[Transaction{id=1, amount=1000.0}, Transaction{id=3, amount=100.0}]"]嵌套异常是 java.lang.NumberFormatException:对于输入字符串:“[Transaction{id=1, amount=1000.0}, Transaction{id=3, amount=100.0}]”]

Here from your HTML you are receiving the Transaction field as a string so in the controller you have to handle that string and extract id and amount from it and then set that id and amount to transaction table.从您的 HTML 中,您将接收交易字段作为字符串,因此在控制器中您必须处理该字符串并从中提取 id 和金额,然后将该 id 和金额设置为交易表。 Try this and if you have already tried or written that in your controller share the code of the controller.试试这个,如果你已经尝试过或在你的控制器中写过,请分享控制器的代码。

暂无
暂无

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

相关问题 [无法将类型&#39;java.lang.String []&#39;的属性值转换为属性的必需类型&#39;java.util.List&#39; - [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 无法将 java.lang.String[] 类型的属性值转换为所需的类型 java.util.List - Failed to convert property value of type java.lang.String[] to required type java.util.List 无法将类型&#39;java.lang.String&#39;的属性值转换为必需类型&#39;java.util.Date&#39; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' 无法将 java.lang.String 类型的属性值转换为所需类型 java.util.Date - Failed to convert property value of type java.lang.String to required type java.util.Date Java_Spring:无法将“java.lang.String”类型的属性值转换为属性“paramsMap”所需的类型“java.util.Map” - Java_Spring: Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'paramsMap' “无法将‘java.lang.String’类型的值转换为所需的‘java.util.List’。” 或者如何将 Python 列表转换为 Java.util 列表? - "Failed to convert value of type 'java.lang.String' to required 'java.util.List'." Or how to convert Python list to Java.util list? 无法将属性“ dateOfBirth”的类型“ java.lang.String”的属性值转换为所需的类型“ java.util.Date”; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfBirth'; 错误:无法将类型java.lang.String的属性值转换为属性dob的必需类型java.util.Date? - Error: Failed to convert property value of type java.lang.String to required type java.util.Date for property dob? 无法将属性fromDate的java.lang.String类型的属性值转换为所需的java.util.Date类型; - Failed to convert property value of type java.lang.String to required type java.util.Date for property fromDate; 使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date] - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM