I'm developing an application using Spring Boot and Thymeleaf, in which I have a Package class and a User class.
Package Class:
@Entity
@Table(name = "table_package")
public class Package {
@Id
@GenericGenerator(name = "gen_id", strategy = "increment")
@GeneratedValue(generator = "gen_id")
private Integer id;
@ManyToOne
@JoinColumn(name = "userid")
@JsonBackReference
private User package_owner;
// private Integer userid;
private String package_name;
@OneToMany(mappedBy = "pkg")
@JsonBackReference
private Set<Article> articles;
@ManyToOne
@JoinColumn(name = "package_statusid")
@JsonBackReference
private PackageStatus packageStatus;
// private Integer package_statusid;
private String notes;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getPackage_owner() {
return package_owner;
}
public void setPackage_owner(User package_owner) {
this.package_owner = package_owner;
}
// public Integer getUserid() {
// return userid;
// }
//
// public void setUserid(Integer userid) {
// this.userid = userid;
// }
public String getPackage_name() {
return package_name;
}
public void setPackage_name(String package_name) {
this.package_name = package_name;
}
public Set<Article> getArticles() {
return articles;
}
public void setArticles(Set<Article> articles) {
this.articles = articles;
}
public PackageStatus getPackageStatus() {
return packageStatus;
}
public void setPackageStatus(PackageStatus packageStatus) {
this.packageStatus = packageStatus;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
@Override
public String toString() {
return "Package{" +
"id=" + id +
", package_owner=" + package_owner +
", package_name='" + package_name + '\'' +
", articles=" + articles +
", packageStatus=" + packageStatus +
", notes='" + notes + '\'' +
'}';
}
}
User Class:
@Entity
@Table(name = "table_user")
public class User {
@Id
@GenericGenerator(name="gen_id" , strategy="increment")
@GeneratedValue(generator="gen_id")
private Integer id;
@OneToMany(mappedBy = "package_owner")
@JsonBackReference
private Set<Package> packages = new HashSet<Package>();
}
I'm coding a screen for the package insert
Package Add/Insert:
<form action="#" th:action="@{/packages/add}" th:object="${Package}" method="post">
<table class="table stripped-table" id="article-table">
<tr>
<th>
User:
</th>
<td>
<input type="text" id="user-search"/>
<input type="text" th:value="*{user.id}" hidden="hidden" th:required="required"/>
</td>
</tr>
<tr>
<th>
Package:
</th>
<td>
<input type="text" th:field="*{package_name}" th:required="required"/>
</td>
</tr>
</table>
<input type="submit" id="submit" value="Add" />
</form>
Before I related the objects between them I was using JavaScript's autocomplete to write the name on the 'user-search', insert the value to the user_id input and just pass that to my controller.
But now that I've created the relation between both objects every time I try to load the template I get this error:
Invalid property 'user' of bean class [com.app.model.Package]: Bean property 'user' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
I've been looking at the getter and setter but can't find an issue. I'm also curious as of how the return should be handled on my controller, but that I assume would be inserting the whole object before the save function on the DB. Any ideas?
Thanks in advance.
EDIT Sorry for the delay, I was on a business trip.
Here's the controller:
@RequestMapping(value = "add", method = RequestMethod.GET)
public ModelAndView addPackageForm(Model model){
List<PackageStatus> packageStatuses = packageStatusRepository.findAll();
model.addAttribute("Package", new Package());
model.addAttribute("PackageStatus", packageStatuses);
ModelAndView mav = new ModelAndView("packages/add");
mav.addObject("page_title", "Test: Add Package");
mav.addObject("template_primary", "packages");
mav.addObject("template_secondary", "add");
return mav;
}
You need to use the proper field name of your class, so switch user
for package_owner
or as you did in your comment, you can do the opposite.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments