当前位置:网站首页>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
边栏推荐
- 对象序列化
- 如何修改 node_modules 里的文件
- yml的配置
- 千万不要错过,新媒体运营15个宝藏公众号分享
- Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
- 干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
- 聊聊 Go 语言与云原生技术
- [high frequency interview questions] difficulty 1.5/5, LCS template questions
- 剑指 Offer 04. 二维数组中的查找
- 手把手带你入门 API 开发
猜你喜欢
How to adjust an integer that is entered in Excel but always displays decimals?
亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?
MySQL高阶语句(一)
动态规划【三】(区间dp)石子合并
MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
Comment modifier Node Fichiers dans les modules
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
esp32s3 IPERF例程测试 esp32s3吞吐量测试
Minimum editing distance (linear DP writing method)
随机推荐
Interviewer: with the for loop, why do you need foreach?
Topic38——56. Consolidation interval
巅峰小店APP仿站开发玩法模式讲解源码分享
What's the matter with Amazon's evaluation dropping and failing to stay? How to deal with it?
R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
在 Golang 中构建 CRUD 应用程序
Safe landing practice of software supply chain under salesforce containerized ISV scenario
Peak store app imitation station development play mode explanation source code sharing
Don't miss it. New media operates 15 treasure official account to share
Time management understood after being urged to work at home
Uni app sends request instructions using the escook / request miniprogram plug-in
hibernate操作oracle数据库 主键自增
Wechat applet payment password input
Research Report on the overall scale, major producers, major regions, products and application segments of swine vaccine in the global market in 2022
mybaitis生成器详解
Building crud applications in golang
alibaba jarslink
MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
Usage of rxjs mergemap