gpt4 book ai didi

java - 将一个实体持久化到另一个实体之后 ~ Netbeans、J2E、bean、EntityManager

转载 作者:太空宇宙 更新时间:2023-11-04 14:18:03 25 4
gpt4 key购买 nike

我正在使用 Java EE、Glassfish 和 Netbeans 做一个软件工程项目,由于缺乏 java 经验,我遇到了一个问题。

我正在开发一个网站,为用户提供创建一些 Activity 的可能性。

我能够在表事件中保存有关事件的信息(ID、名称、地点、日期、创建者),现在的问题是填充另一个表日历.该表中只有 3 个属性:ID(自动且按顺序生成)、创建者电子邮件和事件 ID。此calendar 表的目的是收集所有耦合用户事件:事件不仅可以与其创建者耦合,还可以与其受邀的其他用户耦合。

当我尝试持久保存 calendar 表中的数据时,我收到 NullPointerException 因为在 event 表中生成了 ID自动,我认为当我调用 getEventID 方法时,该值仍然是 null

如何激活 CalendarManager,它只需获取事件的创建者ID 并将它们写入 日历表?

我希望我已经清楚了,这里有一些文件:

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() (getIDEvent 类中是公开的并且是正确的)。系统无法读取 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

编辑1:错误可能出在 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 行,错误是这样的: Caused by: Warning: #{createBean.saveEvent()}: java.lang.NullPointerExceptionjavax.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;
}


}

最佳答案

问题我已经解决了!!

我只是这样修改了日历事件的声明:

private CalendarEvents calendarevents = new CalendarEvents();

我之前没有这样做,我认为如果calendarevents指向NULL它将执行这部分代码:

public CalendarEvents getCalendarEvent() {
if (this.calendarevents == null){
this.calendarevents = new CalendarEvents();
}
return this.calendarevents;
}

public void setCalendarEvents(CalendarEvents calendarevents) {
calendarevents = calendarevents;

}

这是否意味着我不需要最后一部分? :)

关于java - 将一个实体持久化到另一个实体之后 ~ Netbeans、J2E、bean、EntityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27488412/

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