Comments (3)
Full example:
import org.ktorm.database.Database
import org.ktorm.database.Transaction
import org.ktorm.dsl.*
import org.ktorm.entity.*
import org.ktorm.schema.*
import java.time.LocalDate
object Departments : Table<Department>("t_department") {
val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
val location = varchar("location").bindTo { it.location }
}
object Employees : Table<Employee>("t_employee") {
val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
val job = varchar("job").bindTo { it.job }
val managerId = int("manager_id").bindTo { it.manager?.id }
val hireDate = date("hire_date").bindTo { it.hireDate }
val salary = long("salary").bindTo { it.salary }
val departmentId = int("department_id").references(Departments) { it.department }
}
interface Department : Entity<Department> {
companion object : Entity.Factory<Department>()
val id: Int
var name: String
var location: String
}
interface Employee : Entity<Employee> {
companion object : Entity.Factory<Employee>()
val id: Int
var name: String
var job: String
var manager: Employee?
var hireDate: LocalDate
var salary: Long
var department: Department
}
val Database.departments get() = this.sequenceOf(Departments)
val Database.employees get() = this.sequenceOf(Employees)
fun Transaction.ddl(ddl: String) {
connection.prepareStatement(ddl).executeUpdate()
}
fun main() {
val database = Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", user = "root", password = "***")
database.useTransaction {
it.ddl(
"""create table T_EMPLOYEE(
ID int not null primary key auto_increment,
name varchar(128) not null,
job varchar(128) not null,
manager_id int null,
hire_date date not null,
salary bigint not null,
department_id int not null
)"""
)
it.ddl("""
create table t_department(
id int not null primary key auto_increment,
name varchar(128) not null,
location varchar(128) not null
)
""".trimIndent())
for (row in database.from(Employees).select()) {
println(row[Employees.name])
}
val employee = database.employees.find { it.job eq "vince" }
println(employee)
}
}
from ktorm.
H2 treats quoted names and unquoted names differently, quoted names are case-sensitve while unquoted names are case-insensitive, so name
and "name"
are different identifiers, see https://stackoverflow.com/questions/10789994/make-h2-treat-quoted-name-and-unquoted-name-as-the-same
There are 2 options to solve the problem.
Option 1: Use upper-case unquoted names.
CREATE TABLE T_EMPLOYEE(
ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(128) NOT NULL,
JOB VARCHAR(128) NOT NULL,
MANAGER_ID INT NULL,
HIRE_DATE DATE NOT NULL,
SALARY BIGINT NOT NULL,
DEPARTMENT_ID INT NOT NULL
);
object Employees : Table<Employee>("T_EMPLOYEE") {
val id = int("ID").primaryKey().bindTo { it.id }
val name = varchar("NAME").bindTo { it.name }
val job = varchar("JOB").bindTo { it.job }
val managerId = int("MANAGER_ID").bindTo { it.manager?.id }
val hireDate = date("HIRE_DATE").bindTo { it.hireDate }
val salary = long("SALARY").bindTo { it.salary }
val departmentId = int("DEPARTMENT_ID").references(Departments) { it.department }
}
Option 2: Always use quoted names and set alwaysQuoteIdentifiers = true
.
create table "t_employee"(
"id" int not null primary key auto_increment,
"name" varchar(128) not null,
"job" varchar(128) not null,
"manager_id" int null,
"hire_date" date not null,
"salary" bigint not null,
"department_id" int not null
);
object Employees : Table<Employee>("t_employee") {
val id = int("id").primaryKey().bindTo { it.id }
val name = varchar("name").bindTo { it.name }
val job = varchar("job").bindTo { it.job }
val managerId = int("manager_id").bindTo { it.manager?.id }
val hireDate = date("hire_date").bindTo { it.hireDate }
val salary = long("salary").bindTo { it.salary }
val departmentId = int("department_id").references(Departments) { it.department }
}
val database = Database.connect("jdbc:h2:mem:ktorm;DB_CLOSE_DELAY=-1", alwaysQuoteIdentifiers = true)
from ktorm.
Excellent find, I was unaware of this fact, thank you so much for your reply. Tried first option and it worked really well for me. This kind of feels like more of a quirk at H2's side; so I'll close this ticket.
from ktorm.
Related Issues (20)
- 适配 solon框架 HOT 1
- Union功能存在问题? HOT 2
- 关于列绑定,能不能先references再transform?
- 🧐为什么不添加支持分页的函数呢 HOT 2
- Oracle Timestamp support
- Database#exceptionTranslator should return a nullable throwable
- 4.0版本使用KSP生成实体类运行出错 HOT 8
- ksp column converter in version 4.0.0 disappeared HOT 1
- Ktorm 别名机制似乎有问题 HOT 2
- [Feature Request]: Support OffsetDateTime for Postgresql
- [Feature Request]: KSP Table Annotation Support Specify super class for the table generated HOT 1
- Relaxing BaseTable related visibility constraints
- Postgres how to use TextArray's Contains method? HOT 1
- 使用实体查询,框架会拼接字段名称为【表别名_字段】而遭到 ColumnNameChecker 字段名称过长拦截 HOT 6
- Document DDL HOT 2
- Please add support for ILIKE HOT 2
- How to implement bitwise operations? HOT 3
- 4.0+版本使用KSP生成实体类不兼容无符号整数类型 HOT 5
- KSP: Keep entity's deprecation annotations in generated table definitions.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ktorm.