使用Java

时间:2017-05-21 18:39:02

标签: java postgresql hibernate orm

使用Hibernate for ORM时,对象的字段被放置在正确的数据库列中。我提供了有关每个术语应该确切位置的其他信息。

非常感谢任何帮助。

错误

    Hibernate: insert into question (submitted_by, parentCategory, title, 
correct_answer, date_submitted, question_id) values (?, ?, ?, ?, ?, ?)

Failing row contains
(65d275dc-0618-4c54-8fa1-af8ace7e4334 [Should be position 5], 
18230311-95a0-47f8-ba12-937d7b5ef807 [Should be at position 0], 
What is your name [In the right position], Riko [in the right position], 
null [Shouldn't be here], 2017-05-21 13:08:40.534 [Should be position 4] , 
null [Shouldn't be here], 
b8a9cdb6-53d6-4dd6-b07e-31f218b0ce9d [Should be position 1])."

用于在数据库中创建表的脚本

    CREATE TABLE IF NOT EXISTS question (

    question_id uuid NOT NULL ,
    submitted_by uuid REFERENCES app_user ON DELETE SET NULL,
    parentCategory uuid NOT NULL REFERENCES category ON DELETE SET DEFAULT,
    title text NOT NULL,
    correct_answer text NOT NULL,
    date_submitted timestamp NOT NULL DEFAULT now(),

    primary key (question_id)

);

映射文件:

    <class name="cdd.model.Question" table="question">
<!--> Primary Key <!-->
<id column="question_id" name="questionID" type="org.hibernate.type.PostgresUUIDType">
  <generator class="org.hibernate.id.UUIDGenerator"/>
</id>

<many-to-one column="submitted_by" name="submittedBy" not-null="true"/>
<many-to-one column="parentCategory" name="parentCategory" not-null="true"/>

<property column="title" name="title" type="org.hibernate.type.TextType"/>
<property column="correct_answer" name="correctAnswer" type="org.hibernate.type.TextType"/>
<property column="date_submitted" name="dateSubmitted" type="org.hibernate.type.TimestampType"/>
<set cascade="all" name="answers" table="answer">
  <key column="answer_id" not-null="true"/>
  <one-to-many class="cdd.model.Answer"/>

</set>
<join inverse="true" optional="true" table="category_questions">
  <key column="question_id"/>
</join>
<join inverse="true" optional="true" table="accepted_questions_by_user">
  <key column="question_id"/>
</join>
</class>

类别:

    public class Question {
    UUID questionID;
    User submittedBy;
    Category parentCategory;
    String title;
    String correctAnswer;
    Timestamp dateSubmitted; 

    Set<Answer> answers;

    public Question() {       }

    //Constructor, Getters and Setters
} 

正在测试的代码

    public static void main (String[] args){
    Set<Answer> set = new HashSet<>();
    Timestamp t = new Timestamp(System.currentTimeMillis());
    Configuration config = new Configuration().configure("hibernateConfig.cfg.xml");

    //create session factory
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(config.getProperties())
            .build();
    SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);

    //create session
    Session s = sessionFactory.openSession();

    //transaction
    Transaction tx = s.beginTransaction();

    Query c = s.createQuery("from Category where title = 'Software'" );

    Category category = (Category) c.uniqueResult(); 

    Query u = s.createQuery("from User where email = 'admin'");

    User user = (User) u.uniqueResult();

    Question question = new Question(user,category,"What is your name","Riko", t, set);
    question.getAnswers().add(new Answer( "Bob"));
    question.getAnswers().add(new Answer( "Mary"));
    question.getAnswers().add(new Answer( "John"));

    //save an object
    try {
        s.save(question);
        s.flush();
        tx.commit();

        s.close();
    } catch (Exception ex) {
        System.out.println(ex);

    }
}

保存之前打印出Question对象

    Question{questionID=null, 
submittedBy= User{userID=18230311-95a0-47f8-ba12-937d7b5ef807, 
email=admin, fullName=admin,
passwordHash=

sha1:64000:18:EfOsBxPmI1e / NsptE5uM3kViav6QqErs:vod64VtABba / hBOO43J1PPzK,

centre=dd, accessLevel=0, acceptedQuestions=[]}, 

parentCategory=Category{categoryID=b8a9cdb6-53d6-4dd6-b07e-31f218b0ce9d, parent=null, title=Software, description=This category is for all software courses, questions=[]}, 

title=What is your name, correctAnswer=Riko, dateSubmitted=2017-05-21 15:30:58.107, answers=[]}

这是正确的,这使我的问题更加混乱。

0 个答案:

没有答案