因为三天我尝试在DB中插入许多记录,我使用struts 2 and hibernate 3
。我有3个表:
1 Evaluationglobale (id_eval ,label)
2 SousEval(id_eval ,SousItem_ID),
3 SousItems(SousItem_ID,label)
我的问题是当在表Evaluationblobale中使用最后一个插入id_eval
时如何在表SousEval中插入许多记录
为了更多解释,我想做那样的事情:
表评价大全:
id_eval label
----------------------------
1 evaluation 1
表SousItems:
SousItem_ID SousItem_Libelle
----------------------------
1 sousitem 1
2 sousitem 2
3 sousitem 3
4 sousitem 4
我希望在SousEval中插入类似的东西
id_eval SousItem_ID
----------------------------
1 1
1 2
1 3
1 4
而且我不知道如何从这个jsp中从迭代器中恢复所有SousItem_ID
:t我认为我需要递增attribute name
的{{1}},但我不知道如何
这是我的jsp:
<s:select>
我的班级SousEvaluationDao:
<form action="saveOrupdateSousEval" method="post">
-----------------some code ------
<TABLE class="EvalTable" >
<s:iterator value="item" status="userStatus">
<s:select label="%{Item_Libelle}"
headerValue="---------------- Select ---------------"
headerKey="-1"
name="%{sousEvalItem.SousItem_ID}"
list="sousitem"
listKey="SousItem_ID"
listValue="SousItem_Libelle"
cssClass="tdLabelwidht"
value="%{#request.name}"
/>
</s:iterator>
</TABLE>
<s:texfield type=hidden value="%{id_eval}" name="id_eval" />
</form>
在myclass中:
public void saveOrUpdateSousEval(List<SousEvaluation> sousevalnote) {
try {
for (Iterator<SousEvaluation> it = sousevalnote.iterator(); it.hasNext();) {
session.saveOrUpdate(it.next());
}
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}
答案 0 :(得分:0)
您可以单独使用休眠,如果您尝试使用此类数据向表中插入许多值。
创建一个项目并添加所有hibernate所需的jar。因为,我不确定您使用的数据库类型,我可以使用derby数据库提供一个示例。
您需要三个文件: 1)配置文件:
<?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>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="connection.url">jdbc:derby://localhost:1527/EventDB;create=true</property>
<property name="connection.username">user</property>
<property name="connection.password">123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Names the Annotated Entity Class -->
<mapping class="org.EventLogPrint.EventLog"/>
</session-factory>
</hibernate-configuration>
2)您尝试在以下位置插入值的表的类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name="Event_Log")
public class EventLog {
@Id
@Column (name ="Event_Id")
private int event_id;
@Column (name="Run_Id")
private int run_id;
@Column (name="Job_Name")
private String job_name;
public int getEvent_id() {
return event_id;
}
public void setEvent_id(int event_id) {
this.event_id = event_id;
}
public int getRun_id() {
return run_id;
}
public void setRun_id(int run_id) {
this.run_id = run_id;
}
public String getJob_name() {
return job_name;
}
public void setJob_name(String job_name) {
this.job_name = job_name;
}
}
3)插入值的类:
import java.text.ParseException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
public class TestEventLog {
public static void main(String[] args) throws ParseException {
Configuration config = new Configuration();
config.configure();
ServiceRegistryBuilder srb = new ServiceRegistryBuilder().applySettings(config.getProperties());
SessionFactory factory = config.buildSessionFactory(srb.buildServiceRegistry());
Session session = factory.getCurrentSession();
session.beginTransaction();
for(int i = 1; i < 51; i++){
EventLog eventLog = new EventLog();
eventLog.setEvent_id(100+i);
eventLog.setJob_name("job name"+i);
eventLog.setRun_id(1000+i);
session.save(eventLog);
}
session.getTransaction().commit();
}
}
根据您想要输入的对象数量来更改循环值。
不确定这是否是您想采取的方法,但这就是我所知道的一种方式。