我是处理数据库的初学者。目前,我正在使用PostgreSQL数据库。
这是一个帖子请求模型,我在这里得到一个问题列表。
public class QuestionListRequest {
List<QuestionRequest> questionList;
public List<QuestionRequest> getQuestionList() {
return questionList;
}
public void setQuestionList(List<QuestionRequest> questionList) {
this.questionList = questionList;
}
这是我的问题表,我必须存储有关邮寄请求的问题。
@Entity
@Table(name = "ns_question_b")
public class Question extends DomainObject {
@GeneratedValue(strategy = GenerationType.TABLE, generator = "question_gen")
@TableGenerator(name = "question_gen", table = "question_id_gen", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "QuestionId_Gen", initialValue = 100000, allocationSize = 1000)
@Id
@Column(name = "question_id")
private BigInteger questionId;
@Column(name = "question_text")
private String questionText;
@Column(name = "question_type")
@Enumerated(EnumType.STRING)
private QuestionType questionType;
@Column(name = "question_timeout")
private Double questionTimeout;
@Column(name = "marks")
private Double marks;
@Column(name = "negative_mark")
private Double negativeMark;
@Column(name = "question_position")
private Double questionPosition;
@Column(name = "question_options")
private String questionOptions;
@Column(name = "subject")
private String subject;
public Question() {
super();
}
public Question(BigInteger questionId, String questionText, QuestionType questionType, Double questionTimeout, Double marks, Double negativeMark, Double questionPosition, String questionOptions, String subject, Answer answer) {
super();
this.questionId = questionId;
this.questionText = questionText;
this.questionType = questionType;
this.questionTimeout = questionTimeout;
this.marks = marks;
this.negativeMark = negativeMark;
this.questionPosition = questionPosition;
this.questionOptions = questionOptions;
this.subject = subject;
}
public Question(String createdBy, Timestamp creationDate, int version, Timestamp lastModifiedDate,
String lastModifiedBy, RecordStatus recordStatus) {
super(createdBy, creationDate, version, lastModifiedDate, lastModifiedBy, recordStatus);
}
public BigInteger getQuestionId() {
return questionId;
}
public void setQuestionId(BigInteger questionId) {
this.questionId = questionId;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
public QuestionType getQuestionType() {
return questionType;
}
public void setQuestionType(QuestionType questionType) {
this.questionType = questionType;
}
public Double getQuestionTimeout() {
return questionTimeout;
}
public void setQuestionTimeout(Double questionTimeout) {
this.questionTimeout = questionTimeout;
}
public Double getMarks() {
return marks;
}
public void setMarks(Double marks) {
this.marks = marks;
}
public Double getNegativeMark() {
return negativeMark;
}
public void setNegativeMark(Double negativeMark) {
this.negativeMark = negativeMark;
}
public Double getQuestionPosition() {
return questionPosition;
}
public void setQuestionPosition(Double questionPosition) {
this.questionPosition = questionPosition;
}
public String getQuestionOptions() {
return questionOptions;
}
public void setQuestionOptions(String questionOptions) {
this.questionOptions = questionOptions;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
现在我的问题是如何在问题表中逐个存储所有问题与单个数据库调用? 我找到了很多解决方案,但只有行到行映射,我不想要这个。我希望所有问题都能直接存储在一个问题表中,一次调用。
答案 0 :(得分:0)
不清楚你的问题,标题是关于一对多的问题,但解释并不清楚。 如果您需要批量保存对象,可以将所有问题添加到列表中,将其添加到会话中并在特定限制下刷新它。
需要设置此属性
hibernate.jdbc.batch_size 20
并实施以下代码,
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
这取自http://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch15.html