hibernate表在数据库中创建

时间:2017-08-15 00:55:01

标签: java spring hibernate

我的alien.java文件在这里

package com.hibernate;

import javax.persistence.Entity; 

import javax.persistence.Id;

@Entity

public class Alien {

    @Id

    private int aid;

    private String aname;

    private String colour;

    public int getAid() {

        return aid;

    }

    public void setAid(int aid) {

        this.aid = aid;

    }

    public String getAname() {

        return aname;

    }

    public void setAname(String aname) {

        this.aname = aname;

    }

    public String getColour() {

        return colour;

    }

    public void setColour(String colour) {

        this.colour = colour;

    }       
}

App.java文件在这里

package com.hibernate;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class App {

    public static void main(String[] args) {

        Alien dasaradh = new Alien();
        dasaradh.setAid(141);
        dasaradh.setAname("bobby");
        dasaradh.setColour("black");


        Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class);
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(dasaradh);
        tx.commit();
    }

}

enter image description here

探测器没有连接到数据库。

1 个答案:

答案 0 :(得分:0)

看起来您的hibernate.cfg.xml格式错误。

尝试将标题更改为

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
...

或尝试从任何教程中获取正确的xml,并用您的内容替换内容。