我有一个映射到mysql数据库的注释对象。注释包含以下字段:(comment_id,commentText,commentdate)。 comment_id是一个自动生成的整数,而注释文本来自JSF页面的输入。当我调用addComment方法时,commentText按预期填充在数据库中,但是commentdate仍然为null,尽管在方法中设置了(通过创建calculateCurrentDateTime方法的返回值的字符串)。代码如下所示。如果有人能告诉我这是什么问题,我们非常感激。
@ManagedBean
@ViewScoped
public class CommentBean {
private Comment comment;
private static EntityManagerFactory factory;
private static final String PERSISTENCE_UNIT_NAME = "NeedABuilderUnit";
private List<BusinessAccount> businessAccount;
private List<Comment> listOfComments;
@PostConstruct
public void init() {
comment = new Comment();
}
public String addComment(int contractorId) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query myQuery = em.createQuery("SELECT u FROM BusinessAccount u WHERE u.id=:id");
myQuery.setParameter("id", contractorId);
List<BusinessAccount> accounts=myQuery.getResultList();
BusinessAccount account =accounts.get(0);
String date=calculateCurrentDateTime();
comment.setCommentdate(date);
comment.setBusinessAccount(account); //managing both sides
account.getComments().add(comment); //managing both sides
em.persist(comment);
em.getTransaction().commit();
em.close();
comment.setCommentText(null);
return "success";
}
public String calculateCurrentDateTime() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
String currentDate=dateFormat.format(cal.getTime());
return currentDate;
}
@Entity
@Table(name = "comments")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int comment_id;
@Column(name = "comment_text")
private String commentText;
@Column(name = "date")
private String commentdate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "contractor_id", referencedColumnName="id") })
private BusinessAccount businessAccount;
public BusinessAccount getBusinessAccount() {
if (businessAccount == null) {
businessAccount = new BusinessAccount();
}
return businessAccount;
}
public void setBusinessAccount(BusinessAccount businessAccount) {
this.businessAccount = businessAccount;
}
public int getComment_id() {
return comment_id;
}
public void setComment_id(int comment_id) {
this.comment_id = comment_id;
}
public String getCommentText() {
return commentText;
}
public void setCommentText(String commentText) {
this.commentText = commentText;
}
public String getCommentdate() {
return commentdate;
}
public void setCommentdate(String commentdate) {
this.commentdate = commentdate;
}
}
答案 0 :(得分:0)
将日期格式设为
Y-m-d H:i:s
,这是mysql的格式
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
答案 1 :(得分:0)
为什么你不能改变它 @Column(name =“date”) private String commentdate;
到
@Type(类型= “org.jadira.usertype.dateandtime.joda.PersistentLocalDateTimeAsString”) @NotNull private LocalDateTime commentDate;
这样您就可以将currentDate设置为
commentDate = LocalDateTime.now();
这将解决问题,并且无需调用calculateCurrentDateTime()方法,因为LocalDateTime.now()将返回当前日期....