hibernate问题

本人java 新手 现学习hibernate中,感觉非常迷糊,做项目遇到问题,一下午查资料都没解决掉。 上来求帮助 贴代码
Users 类
[code="java"]
public class Users implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6394776786611105432L;
private int id;
private String username;
private String userpwd;
private String usertype;
private Set score = new HashSet();
private String userid;
// public void Users(){

// }

public String getUserid() {
    return userid;
}

public void setUserid(String suseriderid) {
    this.userid = suseriderid;
}


public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getUserpwd() {
    return userpwd;
}

public void setUserpwd(String userpwd) {
    this.userpwd = userpwd;
}

public String getUsertype() {
    return usertype;
}

public void setUsertype(String usertype) {
    this.usertype = usertype;
}



public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Set<Score> getScore() {
    return score;
}


public void setScore(Set<Score> score) {
    this.score = score;
}

}
[/code]

score类[code="java"]
public class Score implements Serializable {

private String username;
private String score;
private int id;
private String userid;
private String examtime;
private String examname;
private Users user;
public Users getUser() {
    return user;
}
public void setUser(Users user) {
    this.user = user;
}
//private Users user;

// public void setUser(Users user) {
// this.user = user;
// }
// public void Score(){
//

// }
public String getUserid() {
return userid;
}

public void setUserid(String userid) {
    this.userid = userid;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getScore() {
    return score;
}

public void setScore(String score) {
    this.score = score;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getExamtime() {
    return examtime;
}

public void setExamtime(String examtime) {
    this.examtime = examtime;
}

public String getExamname() {
    return examname;
}

public void setExamname(String examname) {
    this.examname = examname;
}

}
[/code]

users 映射文件
[code="java"]
<?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">

<class name="bean.usertrain.Users" table="users">

    <id name="id" type="java.lang.Integer">
        <column name="id"/>
            <generator class="sequence">
                <param name="sequence">sequence_name</param>
            </generator>  
    </id>
    <property name="username" type="java.lang.String">
        <column name="username"/>
    </property>
    <property name="userpwd" type="java.lang.String">
        <column name="userpwd"/>
    </property>
    <property name="usertype" type="java.lang.String">
        <column name="usertype"/>
    </property>
     <set name="score"  lazy="false"  inverse="false"   cascade="all"  table="score" >
      <key column="userid"/>
        <one-to-many  class="bean.usertrain.Score"/>
     </set>


</class>

[/code]

score类映射文件
[code="java"]
<?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">

<class name="bean.usertrain.Score" table="score">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="native" />
    </id>
    <property name="username" type="java.lang.String">
        <column name="username" />
    </property>
    <property name="score" type="java.lang.String">
        <column name="score" />
    </property>
    <property name="examtime" type="java.lang.String">
        <column name="examtime" />
    </property>
    <property name="examname" type="java.lang.String">
        <column name="examname" />
    </property>

    <many-to-one name="user" class="bean.usertrain.Users" cascade="none" >   
        <column name="userid" />   
    </many-to-one> 

</class>


[/code]

方法
[code="java"] public boolean save(Score score,String userid) {
Session session= HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
// Users user=(Users)session.load(Users.class, new Integer("29"));
Users user = new Users();
user.setUserid(userid);
score.setUser(user);

    session.save(score);
    tx.commit();
    session.close();
    return true;
}[/code]

主要想 给 score 表中跟user类中添加一条记录,user 表的userid 跟 score 表的userid 要匹配上 我已运行 就报错
错误代码
[code="java"]
严重: Servlet.service() for servlet default threw exception
java.sql.SQLException: ORA-02289: 序列(号)不存在

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:330)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:287)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:742)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:798)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1038)
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:838)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1131)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3284)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3328)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:75)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at dao.exam.ScoreDAOImpl.save(ScoreDAOImpl.java:215)
at action.usertrain.ScoreServlet.addScore(ScoreServlet.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

[/code]

求教 解答
[b]问题补充:[/b]
回汪兆铭 那几个文章我也看了 还是没解决
回lovewhzlq
[code="java"]Users user=(Users)session.load(Users.class, new Integer("29"));

Users user = new Users(); [/code]
这。。 不行吧。 我注掉了 第一句 问题还是那样
[b]问题补充:[/b]
我oracle数据库 我就是看了 之前看了1L给的文章 把NATIVE改成sequence ,因为每次都自动执行一个
[code="java"]select
hibernate_sequence.nextval
from
dual[/code]
语句, 我按照别人写的建了一个[code="java"]create sequence hibernate_sequence start with 1 increment by 1;[/code]

不再报序列号不存在了 但是还有问题。。 这个问题就到这里了 谢谢各位了

7个回答







[code="java"]public boolean save(Score score,String userid) {

Session session= HibernateSessionFactory.getSession();

Transaction tx = session.beginTransaction();

Users user=(Users)session.load(Users.class, new Integer("29"));

Users user = new Users();
user.getScore().add(score);
user.setUserid(userid);

score.setUser(user);

session.save(user);   
tx.commit();   
session.close();   
return true;   

} [/code]

ScoreServlet.java
这个类的63行是什么代码,
我怀疑,可能会不会是Users user=(Users)session.load(Users.class, new Integer("29")); 这里就报错了,你确认下是哪行开始报异常的
如果不是这行的异常,那代码改成

[code="java"]
public boolean save(Score score,String userid) {

Session session= HibernateSessionFactory.getSession();

Transaction tx = session.beginTransaction();

Users user=(Users)session.load(Users.class, new Integer("29"));

user.getScore().add(score);
user.setUserid(userid);   
score.setUser(user);   

session.save(user);   
tx.commit();   
session.close();   
return true;   

}

[/code]

记得配置文件也要修改一下

如下






看一下你的...ctf.xml中的sessionfactory配置是否有问题

什么数据库?
[code="java"]#

sequence_name

[/code]

不是每个数据库都支持的。

[code="xml"]sequence_name [/code]
你这不会直接就写着“sequence_name”吧?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问