gpt4 book ai didi

java - 如何使用Hibernate多对多将jsp页面中多个select标签的数据保存到数据库

转载 作者:行者123 更新时间:2023-11-29 11:45:53 25 4
gpt4 key购买 nike

我想使用hibernate用户保存到数据库中,映射:多对多

POJO's :

用户.java

@Entity
@Table(name = "users_db")
public class Users implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_user", unique = true, nullable = false)
private long id;

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

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

private String password;

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "userskill", joinColumns = {
@JoinColumn(referencedColumnName = "id_user", nullable = false) }, inverseJoinColumns = {
@JoinColumn(referencedColumnName = "id") })
private Set<Skills> skills = new HashSet<Skills>();
// getters ans setters

技能.java

@Entity
@Table(name = "skillsDB", uniqueConstraints = @UniqueConstraint (columnNames ="typeSkill") )
public class Skills implements Serializable {

private static final long serialVersionUID = 1L;

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

@ManyToMany(mappedBy = "skills")
private Set<Users> users = new HashSet<Users>();
// getters and setters

addUserServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserService userService = new UserServiceImpl();

Users user = new Users();

user.setUserName(request.getParameter("UserName"));
user.setEmail(request.getParameter("Email"));
user.setPassword(request.getParameter("Password"));

Set<Skills> skillSet = new HashSet<Skills>();

for (int i = 0; i < request.getParameterValues("skills").length; i++) {
skillSet.add(new Skills(request.getParameterValues("skills")[i]));
}
user.setSkills(skillSet);
userService.add(user);
request.setAttribute("usersList", userService.showUsers());
this.getServletContext().getRequestDispatcher("/addUser.jsp").forward(request, response);

}

UsersDAOImpl.java

public List<Users> showUsers() {

List<Users> result = session.createCriteria(Users.class).list();
return result;

}

Hibernate Configuration

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/dictionnaire</property>
<property name="connection.username">root</property>
<property name="connection.password">mosab</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

<!-- Names the annotated entity class -->
<mapping resource="com.entity.Users" />
<mapping resource="com.entity.Skills" />

</session-factory>

addUser.jsp

In select tag I get data from database

<form action="adduser" method="post">
<table>
<tr>
<td><label for="name">User name :* </label></td>
<td><input type="text" name="UserName" /></td>
</tr>
<tr>

<td><label for="Email">Email :* </label></td>
<td><input type="text" name="Email" /></td>
</tr>
<tr>
<td><label for="Password">Password :*</label></td>
<td><input type="password" name="Password" /></td>
</tr>
<tr>
<td><label for="skills">skills :</label></td>
<td><select name="skills" multiple="multiple">
<c:forEach var="item" items="${skills }">
<option value="${item.typeSkill}" ><c:out value="${item.typeSkill}"/></option>
</c:forEach>
</select></td>
</tr>
<tr>
<td><input type="submit" name="Save" /></td>
</tr>
</table>
</form>
<table>
<thead>
<tr>
<th>Id User</th>
<th>User name</th>
<th>password</th>
<th>email</th>
<th>Skills</th>
</tr>
</thead>
<tbody>
<c:forEach var="item" items="${usersList}">
<tr>
<td><c:out value="${item.id}" /></td>
<td><c:out value="${item.userName}" /></td>
<td><c:out value="${item.password}" /></td>
<td><c:out value="${item.email}" /></td>
<td><c:out value="${item.skills}" /></td>
</tr>
</c:forEach>
</tbody>
</table>

problem : when I fill inputs and I choose some skills, after submit, In the table I get id, name, email, password successfully,but skills apears like this :

如果我选择一项技能:[com.entity.Skills@60b7080b]

如果我选择两种技能:[com.entity.Skills@60b7080b,com.entity.Skills@5e0659ab]

所以我希望您能清楚这一点。

最佳答案

感谢 Dragan Bozanovic 的帮助,但我自己找到了解决方案:

<c:forEach var="item" items="${usersList}">
<tr>
<td><c:out value="${item.id}" /></td>
<td><c:out value="${item.userName}" /></td>
<td><c:out value="${item.password}" /></td>
<td><c:out value="${item.email}" /></td>
<td><c:forEach var="skill" items="${item.skills}">
<c:out value="${skill.typeSkill}"/>
</c:forEach></td>
</tr>
</c:forEach>

关于java - 如何使用Hibernate多对多将jsp页面中多个select标签的数据保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34963107/

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