gpt4 book ai didi

java - 无法将类型 'java.lang.String' 的属性值转换为属性 'java.util.Date' : it is not exactly10characters long 所需的类型 'date'

转载 作者:行者123 更新时间:2023-12-02 09:22:02 24 4
gpt4 key购买 nike

在我的代码中,我有两个实体 BusDetails 和 User。 User 和 BusDetails 具有多对多的关系。每当我尝试预订巴士时,数据都会保存在数据库的联接表中,但我收到此异常:
无法将类型“java.lang.String”的属性值转换为所需类型“java.lang.String”。 util.Date' 属性“日期”;嵌套异常是 java.lang.IllegalArgumentException: 无法解析日期: 它不完全是 10 个字符长]]

用户表:

public class User {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int u_id;

@Column
@NotEmpty(message = "Name cannot be empty")
private String name;

@Column
@NotEmpty(message = "Username cannot be empty")
private String userName;

@Column
@NotEmpty(message = "please enter number")
@Size(min = 10,max = 10, message = "10 digits required")
private String number;

@Column
@NotEmpty
@Size(min=8,message = "Minimum 8 characters required")
private String password;

@ManyToMany(cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
@JoinTable(name = "user_role",joinColumns = @JoinColumn(name = "u_id"), inverseJoinColumns = @JoinColumn(name = "r_id"))
public Set<Role> roles;


@ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
@JoinTable(name = "user_busdetails", joinColumns = @JoinColumn(name = "u_id") , inverseJoinColumns = @JoinColumn(name = "bus_Id"))
public Set<BusDetails> bus = new HashSet<BusDetails>();


//gettersAndSetters

巴士详情:

@Entity
@Component("BusDetails")
public class BusDetails {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int bus_Id;


@Column
public String fromDestination;

@Column
public String toDestination;

@Column
@DateTimeFormat
@Temporal(TemporalType.DATE)
private Date date;

@Column
private String travels;

@Column
private String bus_Type;

@Column
private String seats_Available;

@Column
public String fare;

@Column
private String departure;

@ManyToMany(fetch = FetchType.EAGER,mappedBy = "bus")
@JsonIgnore
public Set<User> user = new HashSet<User>();

//gettersAndSetters

图书 Controller :

@PostMapping("/bookbus")
@ResponseBody
public BusDetails bookBus(@ModelAttribute BusDetails bus) {



System.out.println(bus.getDate());
return busDetail.bookBus(bus);

}
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor( Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-
MM-dd"), true, 10));
}

图书服务:

public BusDetails bookBus(BusDetails bus) {


Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

String currentPrincipleName = authentication.getName();



User user = userRepo.findByUserName(currentPrincipleName);


user.getBus().add(bus);
System.out.println(user);
System.out.println(bus);


userRepo.save(user);



return bus;


}

最佳答案

因为您在 Controller 中使用了@ModelAttribute,意味着所有参数都以字符串格式传递。

在您的情况下,是将String格式化为Date

@Entity
@Component("BusDetails")
public class BusDetails {

//...

@Column
private Date date;

//setter(can add or modify) should be custom like below :
public void setDate(String date){
try {
this.date = new SimpleDateFormat("yyyy-MM-dd").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
}

// ...getter & setter
}

关于java - 无法将类型 'java.lang.String' 的属性值转换为属性 'java.util.Date' : it is not exactly10characters long 所需的类型 'date',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58619965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com