Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate » MS SQL  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 hibernate 在MSSQL中怎么追加记录?我的程序报错了...
farday





发贴: 2
积分: 0
于 2008-07-07 15:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
hibernatedemo .java

package com.farday;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class hibernatedemo {

  public static void main(String[] args) {
    Configuration config = new Configuration();
    config.addClass(userbean.class);
    config.configure();
    
    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.openSession();
    userbean u = (userbean)session.load(userbean.class, new Long(1));
    System.out.println(u.getMsg());
    
    userbean user = new userbean();
    user.setMsg("farday");

     session.beginTransaction();
    session.save(user);
     session.getTransaction();

    session.close();
    factory.close();

  }

}

userbean.java

package com.farday;
public class userbean {
  private Long id;
  private String msg ;
  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
  public Long getId() {
    return id;
  }
  public void setId(Long id) {
    this.id = id;
  }
}

userbean.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
  <class name="com.farday.userbean" table="echo_message">
    <id name="id" type="java.lang.Long" column="id">
      <generator class="native"/>
    </id>
  <property name="msg" type="java.lang.String" column="msg">
     </property>
  </class>

</hibernate-mapping>

hibernate.cfg.xml

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

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
  <property name="myeclipse.connection.profile">
    MyEclipse Derby
  </property>
  <property name="connection.url">
    jdbc:microsoft:sqlserver://127.0.0.1;DatabaseName=Northwind;SelectMethod=cursor
  </property>
  <property name="connection.username">sa</property>
  <property name="connection.password"></property>
  <property name="connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver
  </property>
  <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
  </property>
  <property name="max_fetch_depth">1</property>

</session-factory>

</hibernate-configuration>

报错信息如下:
hello world
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [com.farday.userbean]
  at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
  at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
  at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
  at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
  at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
  at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
  at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
  at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
  at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
  at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
  at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
  at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
  at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
  at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
  at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
  at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:544)
  at org.hibernate.impl.SessionImpl.save(SessionImpl.java:533)
  at org.hibernate.impl.SessionImpl.save(SessionImpl.java:529)
  at com.farday.hibernatedemo.main(hibernatedemo.java:22)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法将 NULL 值插入列 'id',表 'Northwind.dbo.echo_message';该列不允许空值。INSERT 失败。
  at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
  at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
  at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
  at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
  at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
  at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
  at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
  at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
  at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
  at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
  at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
  at com.microsoft.jdbc.base.BaseStatement.executeInternal(Unknown Source)
  at com.microsoft.jdbc.base.BasePreparedStatement.execute(Unknown Source)
  at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1953)
  ... 15 more

表结构如下:
CREATE TABLE echo_message
(
id integer PRIMARY KEY not null,
msg VARCHAR(255)
);

insert into echo_message values(1, 'hello world');
insert into echo_message values(2, 'goodbye world');


不知道要怎么修改,指点一下,初学者.谢谢



作者 Re:hibernate 在MSSQL中怎么追加记录?我的程序报错了... [Re:farday]
farday





发贴: 2
积分: 0
于 2008-07-08 16:48 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
都没有帮忙呀,唉,自己解决了!



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923