如何保存由" user"创建的更改为每个用户分别使用Java / JSF / Hibernate H2 DB

时间:2015-04-09 12:22:17

标签: java hibernate jsf

我有以下问题,我不知道如何解决。

我正在编写一个基本的新闻源agregator webapp,其中“发布者”可以使用新闻制作新主题,读者可以订阅主题,从而阅读他订阅的主题中的新闻。然后他可以将它们标记为已读。

我用Hibernate H2数据库,JSF / Java写这个,我有一个问题。我的代码如下所示:

这是Person的实体:

@Entity
@NamedQuery(name = "findPerson", query = "SELECT e FROM Person e")


public class Person implements Serializable {

private static final long serialVersionUID = 1L;

private static final String TemporalType = null;
@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private Long id;

private String name;
private String pw;
private String checkPw;
private String type;
private String topic;
private ArrayList<String> myTopics;

public Person(String name, String pw) {
    this.name = name;
    this.pw = pw;
}

//setters and getters

这是我的personcontroller类,我有登录和注册方法,他们应该将用户保存在会话中(我希望)。

@Named(value = "personController")
@RequestScoped
@ManagedBean
@SessionScoped
public class PersonController {

@EJB
private PersonEJB personEJB;
private Person person = new Person();
private List<Person> personList = new ArrayList<>();
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
NewsController nc = new NewsController();
private ArrayList<String> myTopics;

public Person login(String name, String pw) {
    Person akt = new Person();

    for (int i = 0; i < getPersonList().size(); ++i) { 
        akt = getPersonList().get(i);
        if (akt.getName().equals(name)) {
            if (akt.getPw().equals(pw)) {
                return akt;
            }
        }
    }
    FacesContext context = FacesContext.getCurrentInstance();
    context.getExternalContext().getSessionMap().put("user", person);
    return null;
}

public boolean register(String name, String pw, String checkPw, String type) {

        for (int i = 0; i < getPersonList().size(); ++i) { 
            if (getPersonList().get(i) instanceof Reader) {
                p = getPersonList().get(i);
                if (p.getName().equals(name))
                    return false;
            }

        }
        break;

    case "Provider":
        p = new Provider(null, null);

        for (int i = 0; i < getPersonList().size(); ++i) { 
            if (getPersonList().get(i) instanceof Provider) {
                p = getPersonList().get(i);
                if (p.getName().equals(name))
                    return false;
            }
        }
        break;
    default:
        return false;

    }

    person.setName(name);
    person.setPw(pw);

    return true;
}

public void subscribeNew() throws IOException {
    if (subscribe(person.getTopic()) == true)
        context.redirect("indexReader.xhtml");
}

public List<Person> getPersonList() {
    personList = personEJB.findPerson();
    return personList;
}

public String addNewPerson() {
    person = personEJB.addPerson(person);
    personList = personEJB.findPerson();
    return "employeeList.xhtml";
}

public void registerNew() throws IOException {

    if (register(person.getName(), person.getPw(), person.getCheckPw(),
            person.getType()) == true) {
        person = personEJB.addPerson(person);
        personList = personEJB.findPerson();
        context.redirect("login.xhtml");
    }

    else {
        context.redirect("register.xhtml");
    }
}

public void loginNew() throws IOException {

    if (login(person.getName(), person.getPw()).getType().equals("Reader")) {
        if (login(person.getName(), person.getPw()) != null)
            context.redirect("indexReader.xhtml");
    } else if (login(person.getName(), person.getPw()).getType().equals(
            "Provider")) {
        if (login(person.getName(), person.getPw()) != null)
            context.redirect("indexProvider.xhtml");
    } else {
        context.redirect("login.xhtml");
    }
}

 public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "login?faces-redirect=true";
    }

//setters, getters

}

EJB看起来像这样:

@Stateless
public class PersonEJB {
    @PersistenceContext(unitName = "user")
    private EntityManager entityManager;

    public List<Person> findPerson() {
        TypedQuery<Person> query = entityManager.createNamedQuery("findPerson",
                Person.class);
        return query.getResultList();
    }

    public Person addPerson(Person person) {
        entityManager.persist(person);
        return person;
    }
}

与新闻类似。

@Entity
@NamedQuery(name = "findNews", query = "SELECT e FROM News e")


public class News implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String text;
private String category;
private String status;

@Temporal(TemporalType.TIMESTAMP)
private Date created = new Date();

@PrePersist
private void onCreate() {
    created = new Date();
}

public News(String text, String category, String status) {
    this.text = text;
    this.category = category;
    this.status = status;
}
//setters, getters

我想要做的是获取一个人想要订阅的主题,然后向该人显示该主题的新闻。我写了这样的东西。

@Named(value = "newsController")
@RequestScoped
@SessionScoped

@ManagedBean(name="news")
public class NewsController implements Serializable{

private static final long serialVersionUID = 1L;
@EJB
private NewsEJB newsEJB;
private News news = new News(null, null, null);
private List<News> newsList = new ArrayList<>();
private List<News> filteredNews = new ArrayList<>();

private List<String> myTopics = new ArrayList<>();

private String topic;
private Person person = new Person();

public boolean subscribe(String topic) {
    for (int i = 0; i < getNewsList().size(); i++) 
        if (getNewsList().get(i).getCategory().contains(topic)) {
            person.getMyTopics().add(topic);
            return true;
        }

            return false;
        }


public List<News> getFilteredNews(){
    News e = new News();
    if(subscribe(topic)==true){
    for(int i =0;i < getNewsList().size(); i++){
        topic = getMyTopics().get(i);
        if (getNewsList().get(i).getCategory().equals(topic)){
            filteredNews = newsEJB.findNews();
            newsEJB.addNews(e);
            filteredNews.add(e);
        }
    }
    }

    return filteredNews;
}


public void subscribeNew() throws IOException{
        getFilteredNews();
        context.redirect("foundTopics.xhtml");
}


public List<News> getNewsList() {
    newsList = newsEJB.findNews();
    return newsList;
}

public void publishNews() throws IOException {
    News akt = new News(news.getText(), news.getCategory(),
            news.getStatus());
    newsList = newsEJB.findNews();
    newsEJB.addNews(akt);
    context.redirect("indexProvider.xhtml");
}

//setters and getters

与上述类似的新闻的EJB类

public class NewsEJB {
    @PersistenceContext(unitName = "user")
    private EntityManager entityManager;

    public List<News> findNews() {
        TypedQuery<News> query = entityManager.createNamedQuery("findNews",
                News.class);
        return query.getResultList();
    }

    public News addNews(News news) {
        entityManager.persist(news);
        return news;
    }
}

boolean subscribe()应该获得我在新​​arraylist中订阅的主题。然后比较getFilteredNews()中此arraylist的主题,并显示这些主题的新闻。并且subscribeNew()获取filteredNews并返回到foundtopics.xhtml。但这始终是一种无效的。

你知道我做错了什么吗?我必须先加入2张桌子吗?我应该怎么做,这个带有订阅主题的arraylist可以为每个人保存?我想每个人都不同。

0 个答案:

没有答案