我正在使用 Java EE、Glassfish 和 Netbeans 进行软件工程项目,但由于缺乏 Java 经验而遇到问题。
我正在开发一个网站,为用户提供创建一些活动的可能性。
我能够将event
有关事件的信息(ID、名称、地点、日期、创建者)保存在一个表中,现在问题是填充另一个表calendar
。在此表中只有 3 个属性:ID(自动和顺序生成)、创建者电子邮件和事件 ID。这个calendar
表的目的是收集所有的用户事件:一个事件不仅可以耦合到它的创建者,还可以耦合到其他被邀请的用户。
当我尝试使表中的数据持久化时,calendar
我得到了一个NullPointerException
,因为在event
表中 ID 是自动生成的,我认为当我调用该getEventID
方法时,这个值仍然是null
.
如何激活一个CalendarManager
只需获取事件的创建者和ID并将它们写入calendar
表中的事件?
我希望我很清楚,这里有一些文件:
EventManager
工作正常:
@Stateless
public class EventManager {
@PersistenceContext
public EntityManager em;
@Inject
Principal principal;
public void saveEvent(Event event) {
em.persist(event);
}
public void deleteEvent(Event event) {
em.remove(event);
}
}
很好,CalendarEventsManager
但没有收到正确的参数:
@Stateless
public class CalendarEventsManager {
@PersistenceContext
public EntityManager cm;
@Inject
Principal principal;
public void saveCalendarEvents(CalendarEvents calendarevents) {
cm.persist(calendarevents);
}
public void deleteCalendarEvents(CalendarEvents calendarevents) {
cm.remove(calendarevents);
}
}
下面是用户单击“创建事件”时激活的 Bean 中的关键部分:
public String saveEvent() {
/* obtain the creator's email */
creator = um.getLoggedUser().getEmail();
this.event.setCreator(creator);
/* save information in db event */
em.saveEvent(event);
/* save information in db calendar */
eventid = event.getID();
this.calendarevents.setUserEmail(creator);
this.calendarevents.setEventID(eventid);
cm.saveCalendarEvents(calendarevents);
/* next page to visualize */
return "displayEvent";
}
直到em.saveEvent(event)
一切正常并且数据库正确更新,但问题是当我这样做时eventid = getID()
(getID
在课堂上是公开的Event
并且是正确的)。系统无法读取 ID,因为它没有从刚刚更新的数据库中读取。
这里有事件类:
@Entity
@Table(name="event")
public class Event implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private Integer id;
@NotNull(message = "May not be empty")
private String name;
@NotNull(message = "May not be empty")
private String place;
@NotNull(message = "May not be empty")
private String eventdate;
@NotNull(message = "May not be empty")
private String type;
@NotNull(message = "May not be empty")
private String visibility;
/*@NotNull(message = "May not be empty")
private String weather;*/
@NotNull(message = "May not be empty")
private String creator;
public void setID(Integer id){
this.id = id;
}
public Integer getID() {
return id;
}
public void setName(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getDate() {
return eventdate;
}
public void setDate(String eventdate) {
this.eventdate = eventdate;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVisibility() {
return visibility;
}
public void setVisibility(String visibility) {
this.visibility = visibility;
}
/*public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}*/
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
你有一些想法来解决这个问题吗?我已经阅读了@PostPersist
注释,但我不知道如何正确应用它:(
感谢所有愿意帮助我的人的建议:D
EDIT1:错误可能在 saveEvent() 中的这段代码中:
this.calendarevents.setUserEmail(creator);
this.calendarevents.setEventId(eventid);
System.out.println("event id after get is: " + this.calendarevents.getUserEmail());
System.out.println("event id after get is: " + this.calendarevents.getEventId());
cm.saveCalendarEvents(calendarevents);
我已将 system.out.println 用于验证该值,但错误出现在“this.calendarevents”中,因为没有 system.out 的迹象!这里有完整的 Bean!
@Named(value = "createBean")
@RequestScoped
public class CreateEventBean {
@EJB
private EventManager em;
@EJB
private UserManager um;
private Event event;
private String creator;
@EJB
private CalendarEventsManager cm;
private Integer eventid=0;
private CalendarEvents calendarevents;
public CreateEventBean() {
}
public Event getEvent() {
if (event == null){
event = new Event();
}
return event;
}
public void setEvent(Event event) {
this.event = event;
}
public CalendarEvents getCalendarEvent() {
if (calendarevents == null){
calendarevents = new CalendarEvents();
}
return calendarevents;
}
public void setCalendarEvents(CalendarEvents calendarevents) {
this.calendarevents = calendarevents;
}
public String saveEvent(){
/*obtain the creator's email*/
creator=um.getLoggedUser().getEmail();
this.event.setCreator(creator);
/*save information in db event*/
em.saveEvent(event);
/*save information in db calendar*/
System.out.println("event id before get is: " + eventid);
eventid=event.getId();
System.out.println("event id after get is: " + eventid);
this.calendarevents.setEventId(eventid);
System.out.println("eventid in calendar could be: " + this.calendarevents.getEventId());
this.calendarevents.setUserEmail(creator);
System.out.println("useremail in calendar could be: " + this.calendarevents.getUserEmail());
cm.saveCalendarEvents(calendarevents);
/*next page to visualize*/
return "displayEvent";
}
}
我真的没有发现“this.calendarevents.setEventId(eventid);”中的错误 那是第 80 行,错误是: 引起:警告:#{createBean.saveEvent()}:java.lang.NullPointerException javax.faces.FacesException:#{createBean.saveEvent()}:java.lang.NullPointerException [引起:java.lang.NullPointerException at it.nyquistmeteocal.beans.CreateEventBean.saveEvent(CreateEventBean.java:80)]
这是 CalendarEvents 类:
@Entity
@Table(name="calendarevents")
public class CalendarEvents implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private Integer id;
@NotNull(message = "May not be empty")
private String useremail;
@NotNull(message = "May not be empty")
private Integer eventid;
public Integer getId() {
return id;
}
public void setName(Integer id) {
this.id = id;
}
public String getUserEmail() {
return useremail;
}
public void setUserEmail(String useremail) {
this.useremail = useremail;
}
public Integer getEventId() {
return eventid;
}
public void setEventId(Integer eventid) {
this.eventid = eventid;
}
}