日期:2014-05-18 浏览次数:20710 次
package com.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/*
* 使用注解创建并关联数据库中的表,需要在hibernate配置文件中设置映射
*/
@Entity
public class Weapon {
private long id;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
//@SequenceGenerator(name="weapon_seq")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<mapping class="com.entity.Weapon"/>
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import com.entity.Weapon;
/*
* 通过在hibernate文件中注册实体bean,并在实体bean中添加
* hibernate注解,可以自动创建数据库中的关联表并将属性与字段关联
*/
public class TestAnnotations {
private static AnnotationConfiguration ac = new AnnotationConfiguration().configure();
private static SessionFactory sf = null;
public static void main(String[] args) {
sf = ac.buildSessionFactory();
Session session = sf.openSession();
Transaction tran = session.beginTransaction();
Weapon weapon = new Weapon();
weapon.setName("jack");
weapon.setId(1);
session.save(weapon);
tran.commit();
session.close();
}
}
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Weapon (name, id) values (?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hiber