无法为同一个问题提取不同的代码

时间:2018-04-16 06:22:11

标签: java list hibernate web-applications tuples

我试图获取存储在数据库中的特定questionId的标签, 问题是,它显示一个标签名称而不是不同的标签。(对于questionId:5010它有2个标签java和Php,而在该问题下面它只显示java两次)

    public class TagDAOImpl implements TagDAO {
    public List<String> viewQuestion(String question) throws Exception {
        List<String> quesString = null;
        SessionFactory sessionFactory = null;
        Session session = null;

        try {
            quesString = new ArrayList<String>();
            sessionFactory = HibernateUtility.createSessionFactory();
            session = sessionFactory.openSession();
            QuestionEntity tag = new QuestionEntity();
            // Question q=new Question();
            // q.setTagId(question);
            // ques.setQuestionString(question.getQuestionString());
            System.out.println("dao" + question);

            // session.persist(ques);

            session.getTransaction().begin();

            Query query = session
                    .createQuery("from QuestionEntity where tagId=?");
            query.setString(0, question);
            List<QuestionEntity> custList = query.list();

            List list = query.list();
            for (QuestionEntity tagEntity : custList) {
                System.out
                        .println("Question ID=>" + tagEntity.getQuestionId()
                                + "\nQuestion String=>"
                                + tagEntity.getQuestionString());
                List<String> newList = new ArrayList<String>();
                newList.addAll(getTag(tagEntity.getQuestionId()));

                quesString.add(tagEntity.getQuestionString());
                quesString.addAll(newList);

            }
            session.getTransaction().commit();

        } catch (HibernateException exception) {
            exception.printStackTrace();
            DOMConfigurator.configure("src/com/infy/resources/log4j.xml");
            Logger logger = Logger.getLogger(this.getClass());
            logger.error(exception.getMessage(), exception);
            throw new Exception("DAO.TECHNICAL_ERROR");
        } catch (Exception exception) {
            exception.printStackTrace();
            DOMConfigurator.configure("src/com/infy/resources/log4j.xml");
            Logger logger = Logger.getLogger(this.getClass());
            logger.error(exception.getMessage(), exception);
            throw exception;
        } finally {
            if (session.isOpen() || session != null) {
                session.close();
            }
        }
        return quesString;
    }

    // quesString.add();

    // quesString.add("\n");

    public List<String> getTag(int question) throws Exception {
        List<String> quesString = null;
        SessionFactory sessionFactory = null;
        Session session = null;

        try {
            quesString = new ArrayList<String>();
            sessionFactory = HibernateUtility.createSessionFactory();
            session = sessionFactory.openSession();
            QuestionEntity tag = new QuestionEntity();
            // Question q=new Question();
            // q.setTagId(question);
            // ques.setQuestionString(question.getQuestionString());
            System.out.println("dao" + question);

            // session.persist(ques);

            session.getTransaction().begin();

            Query query2 = session
                    .createQuery("from QuestionEntity where questionId=?");
            query2.setInteger(0, question);
            List<QuestionEntity> tagList = query2.list();
            for (QuestionEntity tagEntity : tagList)
                System.out.println("custlist " + tagEntity.getTagId());

            // List list = query2.list();
            for (QuestionEntity tagEntity : tagList) {
                System.out.println("Question ID=>" + tagEntity.getQuestionId()
                        + "\nQuestion String=>" + tagEntity.getQuestionString()
                        + "\ntagId=>" + tagEntity.getTagId());
                quesString.add(tagEntity.getTagId());
                System.out.println(tagEntity.getTagId());

            }
            session.getTransaction().commit();

            // quesString.add();

            // quesString.add("\n");

        } catch (HibernateException exception) {
            exception.printStackTrace();
            DOMConfigurator.configure("src/com/infy/resources/log4j.xml");
            Logger logger = Logger.getLogger(this.getClass());
            logger.error(exception.getMessage(), exception);
            throw new Exception("DAO.TECHNICAL_ERROR");
        } catch (Exception exception) {
            exception.printStackTrace();
            DOMConfigurator.configure("src/com/infy/resources/log4j.xml");
            Logger logger = Logger.getLogger(this.getClass());
            logger.error(exception.getMessage(), exception);
            throw exception;
        } finally {
            if (session.isOpen() || session != null) {
                session.close();
            }
        }
        return quesString;
    }

}

它为一个问题ID显示两个相同的标签。 我需要将Java和Php作为questionid 5010的标签。 在表脚本中它工作正常但在DAO函数中它无法正常工作。 enter image description here

Questionid和tagid是复合主键。

  

插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39;什么是java和javascript?&#39;,&#39; Java&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39;什么是java和javascript?&#39;,&#39; Java脚本&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39;什么是java和javascript?&#39;,&#39; Python&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39;谁是python?&#39;,#39; Python&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39;什么是HTML?&#39;,&#39; HTML&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39;什么是Java?&#39;,&#39; Java&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39; post和get之间有什么区别?&#39;,#Java; Java Script&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39; java中的静态变量是什么?&#39;,#39; Java&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39;我可以在Java中调用/引用groovy静态内部类,&#39; Java&#39;);   插入问题值(DBquestionId.NEXTVAL,&#39; U1&#39;,&#39;如何为没有eclipse运行的eclipse创建可执行JAR文件&#39;,Java&#39;);   插入问题值(&#39; 5010&#39;,&#39; U1&#39;,&#39;如何为没有eclipse运行的eclipse创建可执行的JAR文件&#39;,&#39; Php& #39);

0 个答案:

没有答案