donalmcm:
私はadmin.htmlページに移動し、自分のアプリケーションを実行することによってこれをテストしようとします。私は、フォームの最初の名前の値は、最後の名前とメールアドレスを記入してください。送信ボタンをクリックするだけで、エラーは「nullにすることはできません列の電子メール」のために表示されます。私は簡潔にするため、このようなcontructorsなどゲッター、セッター、などのコードを除外しています。これは私が、私は値が従業員オブジェクトを作成するために使用されている私のAPIへのポストの値に使用するフォームを持っている私のadmin.htmlページです
<form role="form" action="api/employees/create" method="post">
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" class="form-control" id="firstName" placeholder="Enter first name">
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" class="form-control" id="lastName" placeholder="Enter last name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" placeholder="Enter email">
</div>
<button type="submit" class="btn btn-success btn-block">Create</button>
</form>
これは私がポストを処理し、フォームから渡された値を持つオブジェクトを作成し、この新しいオブジェクトを永続化しようとするEmployeeAPI.javaクラスで私のPOSTメソッドであります
@POST
@Path("create")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
public Response createEmployee(@FormParam(value = "firstName") String firstName,
@FormParam(value = "lastName") String lastName,
@FormParam(value = "email") String email) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.getCurrentSession();
URI location;
try{
session.getTransaction().begin();
Employee newEmployee = new Employee();
newEmployee.setFirstName(firstName);
newEmployee.setLastName(lastName);
newEmployee.setEmail(email);
session.persist(newEmployee);
session.getTransaction().commit();
session.close();
location = new URI("http://localhost:8080/index.html");
return Response.temporaryRedirect(location).build();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
}
return null;
}
私はただのfirstName、lastNameの、電子メールおよびすべての値に対して1を持つ従業員のためのコンストラクタを持っている - これが私のEmployee.javaモデルクラスです。
@XmlRootElement
@Entity
public class Employee {
@Id
@GeneratedValue
private int id;
@Expose
@Column(nullable = false)
private String firstName;
@Expose
@Column(nullable = false)
private String lastName;
@Expose
@Column(nullable = false, unique = true)
private String email;
これは私が私のサーバー側で見ていますエラーです
Hibernate: insert into Employee (availability_id, email, firstName, isAdmin, isManager, isMentee, isMentor, lastName, mentorDuration, topic_name, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2019-03-04 16:07:41 WARN SqlExceptionHelper:129 - SQL Error: 1048, SQLState: 23000
2019-03-04 16:07:41 ERROR SqlExceptionHelper:131 - Column 'email' cannot be null
2019-03-04 16:07:41 INFO AbstractBatchImpl:193 - HHH000010: On release of batch it still contained JDBC statements
2019-03-04 16:07:41 ERROR ExceptionMapperStandardImpl:39 - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]