Add the following URL to your Eclipse Neon 4.6.1 installation, via:
Help > Install New Software… > Work with:
http://download.jboss.org/jbosstools/neon/stable/updates/
Then select the individual features that you want to install:
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
123456
jdbc:mysql://localhost:3306/test
root
org.hibernate.dialect.MySQLDialect
update
true
package Hibernate;
public class News {
private Integer id;
private String title;
private String content;
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return this.id;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return this.content;
}
}
package Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class NewsManager {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();//包含基本配置信息(hibernate.cfg.xml)和对象映射关系信息(News.hb,.xml)
SessionFactory sessionFactory = configuration.buildSessionFactory();//创建SessionFactory,用来生成Session
Session session = sessionFactory.openSession();//生成一个session
Transaction transaction = session.beginTransaction();//开始一个事务
News news = new News();//创建一个要进行持久化的对象
news.setId(1);
news.setContent("测试内容");
news.setTitle("标题");
session.save(news);//进行保存
transaction.commit();//提交事务
session.close();//关闭Session
sessionFactory.close();//关闭SessionFactory
}
}
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jialong1997
jdbc:mysql://localhost:3306/test
root
org.hibernate.dialect.MySQL5Dialect
update
true
com.mysql.jdbc.Driver
jialong1997
jdbc:mysql://localhost:3306/test
root
org.hibernate.dialect.MySQLDialect
update
true
package test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="news_inf")
public class News {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String title;
private String content;
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return this.id;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return this.content;
}
}