当前位置:网站首页>Hibernate operation Oracle database primary key auto increment
Hibernate operation Oracle database primary key auto increment
2022-06-27 12:19:00 【Jinlin】
Believed to have been used mysql,sql server,oracle Our friends all know ,oracle If you want to set the primary key of the table in to self growth , Then you need to create a sequence
stay oracle In Chinese, it means
create table Student(
Student_ID number(6) NOT NULL PRIMARY KEY,
Student_Name varchar2(10) NOT NULL,
Student_Age number(2) NOT NULL
);
CREATE SEQUENCE student_sequence
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
CACHE 10;
insert into Student values(student_sequence.nextval,'aa',20);
A piece of data will be saved and inserted in the database id by (2,aa,20) The data of
With this knowledge and hibernate Operation on Database , We can start writing code
Uncommented version student JavaBean Class is
package com.bean;
public class Student
{
private int student_id;
private String student_name;
private int student_age;
public int getStudent_id()
{
return student_id;
}
public String getStudent_name()
{
return student_name;
}
public int getStudent_age()
{
return student_age;
}
public void setStudent_id(int id)
{
this.student_id = id;
}
public void setStudent_name(String name)
{
this.student_name = name;
}
public void setStudent_age(int age)
{
this.student_age = age;
}
}
Corresponding student.hbm.xml File core code
<class name="Student" table="Student">
<id name="student_id" column="student_id" type="java.lang.Integer">
<generator class="native">
<param name="sequence">student_sequence</param>
</generator>
</id>
<property name="student_name" column="Student_Name" type="java.lang.String"/>
<property name="student_age" column="Student_Age" type="java.lang.Integer"/>
</class>
Annotated entity class code
package com.bean;
import javax.persistence.*;
@Entity
@Table(name="student")
public class StudentAnnotation {
private int student_id;
private String student_name;
private int student_age;
@Id
@SequenceGenerator(name="generator",sequenceName="student_sequence")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="generator")
/*@Id
@GenericGenerator(name = "idGenerator", strategy = "native")
@GeneratedValue(generator = "idGenerator") Some netizens said that this method can also , But it's actually wrong */
public int getStudent_id()
{
return student_id;
}
@Column
public String getStudent_name()
{
return student_name;
}
@Column
public int getStudent_age()
{
return student_age;
}
public void setStudent_id(int id)
{
this.student_id = id;
}
public void setStudent_name(String name)
{
this.student_name = name;
}
public void setStudent_age(int age)
{
this.student_age = age;
}
}
Of course , The configuration file hibernate.cfg.xml The code is as follows ,oracle The database for 11g
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property><!-- Database connection url-->
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:orcl</property>
<property name="connection.username">go</property>
<property name="connection.password">go</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- <mapping resource="com/bean/Student.hbm.xml"/> --> <!-- Use *.hbm.xml Please keep this line , Otherwise, note -->
<mapping class="com.bean.StudentAnnotation" /> <!-- Please keep this line for annotation , Otherwise, note -->
</session-factory>
</hibernate-configuration>
Finally, the test class
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
//import com.bean.Student; // No comment
import com.bean.StudentAnnotation; // There are notes
public class Test {
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
StudentAnnotation stu = new StudentAnnotation(); // There are notes
//Student stu = new Student(); // No comment
stu.setStudent_name("zhangsan");
stu.setStudent_age(18);
session.save(stu);
tx.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
After testing , There should be... In the database zhagnsan Student information
The database driver tested is ojdbc-14 Version of , When it comes to testing , There will be a INFO Information ,
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Can be ignored , On the Internet ojdbc-6 There's no problem with the new version , Positive solution , But the data in the database is
https://blog.csdn.net/thepeakofmountain/article/details/17173715
边栏推荐
- 自学ADT和OOP
- 微服务拆分
- Jianmu continuous integration platform v2.5.0 release
- nifi从入门到实战(保姆级教程)——身份认证
- Interview shock 60: what will cause MySQL index invalidation?
- esp32s3 IPERF例程测试 esp32s3吞吐量测试
- 2022ciscn central China Web
- alibaba jarslink
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用VGAM包的vglm函数对有序多分类logistic回归模型进行平行性假设作检验
- log4j.properties的配置详解
猜你喜欢

解开C语言的秘密《关键字》(第六期)

Peak store app imitation station development play mode explanation source code sharing

How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line

Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)

Histrix工作原理

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article

私藏干货分享:关于企业架构中如何进行平台化

巅峰小店APP仿站开发玩法模式讲解源码分享

TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
Interview shock 60: what will cause MySQL index invalidation?
随机推荐
57. The core principle of flutter - layout process
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the AIC function is used to compare the AIC values of the two models (simple
Wechat applet realizes five-star evaluation
ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
Getting started with go web programming: validators
MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
Jwas: a Bayesian based GWAS and GS software developed by Julia
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、解读分析模型
Don't miss it. New media operates 15 treasure official account to share
build.gradle 配置
MapReduce principle analysis (in-depth source code)
Private dry goods sharing: how to implement platform in Enterprise Architecture
C# wpf 实现撤销重做功能
Interview shock 60: what will cause MySQL index invalidation?
mysql学习1:安装mysql
关于枚举类的两种用法
master公式
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
Histrix工作原理