为什么我的数据没有使用Intellij插入数据库但在Eclipse中有效?

时间:2016-09-02 14:48:32

标签: hibernate intellij-idea mysql-workbench

我在Intellij Idea平台上尝试了使用Java中的hibernate进行一对多映射的示例。其他hibernate示例正在工作,数据被插入到数据库中。

当我尝试将数据插入数据库以进行一对多映射时,会创建表,但不会插入数据。当我运行main(测试)文件时,会生成以下消息。

Hibernate: alter table StudentDetail_Student drop foreign key FK_h20lycslt41w9ybbid3wsc31
Hibernate: alter table StudentDetail_Student drop foreign key FK_8n7b6xvo3mer5vs0b4i7t90rt
Hibernate: drop table if exists Student
Hibernate: drop table if exists StudentDetail
Hibernate: drop table if exists StudentDetail_Student
Hibernate: create table Student (Student_ID integer not null auto_increment, Student_Name varchar(255), primary key (Student_ID))
Hibernate: create table StudentDetail (Student_ID integer not null auto_increment, Mobile_number varchar(255), primary key (Student_ID))
Hibernate: create table StudentDetail_Student (StudentDetail_Student_ID integer not null, student_Student_ID integer not null)
Hibernate: alter table StudentDetail_Student add constraint UK_h20lycslt41w9ybbid3wsc31  unique (student_Student_ID)
Hibernate: alter table StudentDetail_Student add constraint FK_h20lycslt41w9ybbid3wsc31 foreign key (student_Student_ID) references Student (Student_ID)
Hibernate: alter table StudentDetail_Student add constraint FK_8n7b6xvo3mer5vs0b4i7t90rt foreign key (StudentDetail_Student_ID) references StudentDetail (Student_ID)

当我检查数据库的表时,没有插入数据。为此,一对多映射,我写了3个类:Main类,Student类和StudentDetails类。学生班有StudentId和学生姓名。 StudentDetails有StudentId和MobileNumber。

我在StudentDetails课程中创建了学生列表。在主课程中,我创建了两个Student类对象并将它们传递给列表..

但是,我所接受的学生和手机号码的情况并不恰当,但只想测试。

我的主要课程是:

    package hibernateProject.com.himal.studentOneToManyMapping;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Himal Acharya on 2016-09-02.
 */
public class Main {
    public static void main(String[] args) {
       StudentDetail studentDetail=new StudentDetail();
        studentDetail.setMobileNumber("9846266378");

        Student student1=new Student();
        student1.setStudentName("Himal");

        Student student2=new Student();
        student2.setStudentName("Shyam");

        List<Student> studentList=new ArrayList<>();
        studentList.add(student1);
        studentList.add(student2);

        studentDetail.setStudent(studentList);

        Session session=null;
        Transaction txn=null;

        try{
            SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
            session=sessionFactory.openSession();
            txn=session.beginTransaction();

            session.save(studentDetail);
            session.save(student1);
            session.save(student2);

            session.getTransaction().commit();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally {
            if(!txn.wasCommitted()){
                txn.rollback();
            }
            session.flush();
            session.close();
        }



    }
}

学生班级代码是:

package hibernateProject.com.himal.studentOneToManyMapping;

import javax.persistence.*;

/**
 * Created by Himal Acharya on 2016-09-02.
 */

@Entity

public class Student {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Student_ID")
    private int studentID;

    @Column(name="Student_Name")
    private String studentName;

    public int getStudentID() {
        return studentID;
    }

    public void setStudentID(int studentID) {

        this.studentID = studentID;
    }

    public String getStudentName() {

        return studentName;
    }

    public void setStudentName(String studentName) {

        this.studentName = studentName;
    }


}

StudentDetails课程是:

package hibernateProject.com.himal.studentOneToManyMapping;


import javax.persistence.*;
import java.util.List;

/**
 * Created by Himal Acharya on 2016-09-02.
 */
@Entity
public class StudentDetail {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="Student_ID")
    private int studentID;
    @Column(name="Mobile_number")
    private String mobileNumber;

    @OneToMany
    private List<Student> student;

    public int getStudentID() {

        return studentID;
    }

    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public List<Student> getStudent() {

        return student;
    }

    public void setStudent(List<Student> student) {

        this.student = student;
    }
}

我的hibernate.config.xml位于src目录下:该xml的某些部分是:

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->

        <mapping class="hibernateProject.com.himal.studentOneToManyMapping.Student"/>
        <mapping class="hibernateProject.com.himal.studentOneToManyMapping.StudentDetail"/>

相同的代码在Eclipse中工作,数据插入到Eclipse的数据库中,但是通过Intellij创建了空表。

0 个答案:

没有答案