当前位置:网站首页>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
边栏推荐
- threejs的环境光+点光源+平行光源+球面光 以及hepler理解+阴影()
- Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
- Daily leetcode force deduction (21~25)
- mybaitis生成器详解
- R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.labels参数在可视化图像中为强调线添加标签信息
- On ticheck
- The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, specifies the grouping parameters with the b
- C# wpf 实现撤销重做功能
- 优博讯出席OpenHarmony技术日,全新打造下一代安全支付终端
- 56. Core principle of flutter - flutter startup process and rendering pipeline
猜你喜欢

Neo4j:入门基础(一)之安装与使用

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

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

Excel中输入整数却总是显示小数,如何调整?

剑指 Offer 04. 二维数组中的查找

Microservice splitting

c/s 架构

数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题

Comment modifier Node Fichiers dans les modules

面试突击60:什么情况会导致 MySQL 索引失效?
随机推荐
Mit6.031 software construction7 reading notesdesigning specifications
i. Construction of mx6ull C language environment
Dynamic programming [III] (interval DP) stone merging
Rxjs mergeMap 的使用场合
解压 .log.gz 文件
对象序列化
.NET6接入Skywalking链路追踪完整流程
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、解读分析模型
Usage of rxjs mergemap
Interviewer: with the for loop, why do you need foreach?
Master formula
i.mx6ull(单片机) c语言环境搭建
Drive to APasS! Use Mingdao cloud to manage F1 events
StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior
Minimum editing distance (linear DP writing method)
Tidb 6.0: making Tso more efficient tidb Book rush
树莓派 3b+ 学习
自学ADT和OOP
Dynamic programming [4] (counting class DP) example: integer partition
深入理解 happens-before 原则