@Controller
@RequestMapping("clients")
public class ClientController {
@Autowired
private ClientDao clientDao;
@Autowired
private AddressDao addressDao;
@RequestMapping(value = "")
public String index(Model model) {
model.addAttribute("clients", clientDao.findAll());
model.addAttribute("title", "My Clients");
return "clients/index";
}
@RequestMapping(value = "add", method = RequestMethod.GET)
public String displayAddClientForm(Model model) {
model.addAttribute("title", "Add Client");
model.addAttribute(new Client());
return "clients/add";
}
@RequestMapping(value = "add", method = RequestMethod.POST)
public String processAddClientForm(@ModelAttribute @Valid Client newClient,
Errors errors, Model model) {
if (errors.hasErrors()) {
model.addAttribute("title", "Add Client");
model.addAttribute("client", newClient);
return "clients/add";
}
clientDao.save(newClient);
return "redirect:";
}
}
客户端
实体:
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue
@Column(name = "client_id")
private int id;
@NotNull
@Size(min=1, message = "Client must have a first name")
@Column(name = "first_name")
private String firstName;
@NotNull
@Size(min=1, message = "Client must have a last name")
@Column(name = "last_name")
private String lastName;
@OneToOne(cascade = CascadeType.ALL,
fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "address_id")
private Address address;
@NotNull
@Size(min=10, max=10, message = "Must be 10 digits only")
private String phone;
// setters/getters
}
地址
实体:
@Entity
@Table(name = "address")
public class Address {
@Id
@GeneratedValue
@Column(name = "address_id")
private int id;
@NotNull
@Size(min=1, message = "Can not be empty")
@Column(name = "street")
private String street;
@NotNull
@Size(min=1, message = "Can not be empty")
@Column(name = "city")
private String city;
@NotNull
@Enumerated(EnumType.STRING)
@Column(name = "state")
private State state;
@NotNull
@Size(min=5, max=5, message = "Invalid")
@Column(name = "zip_code")
private String zipCode;
@OneToOne(mappedBy = "address" , fetch = FetchType.EAGER)
//@JoinColumn(name = "client_id")
public Client client;
// setters and getters
}
在客户端
类中,将@valid
添加到address
字段:
@Valid
@OneToOne(cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
optional = false)
@JoinColumn(name = "address_id")
private Address address;
@valid
javadoc说:
公共@接口有效
警告conda . exceptions:print _ unexpected _ error _ report(1216):key error(' pkgs _ dirs ')trace back(最近一次调用last):File " C:\ program data \ anaconda 3 \ lib \ site-packages \ conda \ exceptions . py ",第
使用hibernate时,我陷入了模式验证问题。错误很简单,它说我的代码中预期的列类型和实际找到的列类型不匹配。 但据我所知,他们确实匹配。在看了类似的问题后,我添加了columnDefinition=“int(10)”,但这没有帮助。如果您能提供任何帮助或指点,我们将不胜感激。 类定义和变量声明 Traject类 Toetsbijtrajet类 表traject 错误消息:
注意:试图访问第18行C:\xampp\htdocs\E-LAMAX\app\libraries\Core.php中null类型值的数组偏移量 致命错误:未捕获PDOException:SQLSTATE[42S02]:找不到基表或视图:1146表“lamax.settings”在C:\xampp\htdocs\E-lamax\app\libraries\Database.php中不存在。php:6
问题内容: 我可以在Hibernate中使用MySQL视图,方法是将它们视为表-即。该实体与为表创建的实体相同。但是,当将Hibernate设置为验证模型时,我的应用程序将不会部署,因为它假设它是表就找不到视图。 是否可以在启用部署时间验证的情况下使用Hibernate实体(hibernate.hbm2ddl.auto = validate)? 谢谢。 问题答案: JIRA中对此有几个问题( HH
当我在Hibernate中测试@OneToOne的映射关系的一些东西时,我使用spring-data-jpa进行查询。对于@OneToOne的双向关系,当我查询一个实体时,会出现两种情况: 当两个表中没有数据时,没有错误 当数据存储在两个表中时,只需StackOverflowerr 接下来的相关代码: 佩尔松道: 身份证道: 测试代码: 我在网站上搜索了一些答案,找到了一个相关的问题,StackO
根据文档,我需要重新验证用户谁试图删除他们的帐户,但没有登录一段时间。下面是文档(非常底部): 文件编制 如果执行其中一个操作,而用户登录时间太长,则操作失败并出现错误。当发生这种情况时,通过从用户获取新的登录凭据并将凭据传递给reauthenticateWithCredential来重新验证用户 请帮忙。