Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A code-first object-relational model for accessing SQL data from Java

License

NotificationsYou must be signed in to change notification settings

HeliORM/HeliORM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java CI with Maven

HeliORM is acode-first object-relational model for accessing SQL data from Java. It allowscompile time typesafe SQL queries from Java code.

Here is an example of a type safe query in Java:

List<Cat>all =orm().select(CAT)                .where(CAT.type.eq(CatType.INDOOR))                .join(PERSON).on(CAT.personId,PERSON.id)                .where(PERSON.emailAddress.eq(person.getEmailAddress()))                .orderBy(CAT.name,CAT.age.desc())                .list();

If you find this example interesting, read on.

The idea is that you write your plain old Java Objects (POJOs), and then generate a supporting data model that allowstype-safe queries, and then use this model and a very simple API to do Structured Query Language (SQL) queries andupdates. It is focussed on working with POJOs and is intended to make create, update, read, delete (CRUD) operationseasy. It is not meant to be a complete implementation of SQL in Java.

HeliORM also supports querying on abstract data types.

⚠️ This page is currentlyvery incomplete

Getting HeliORM

I recommend using Maven or your prefered package management technology to add HeliORM To your project. You'll need atleast thecore library, and most likely a SQL driver library, and if you wish to use annotations in your POJOs, theannotation library.

The core library

  <dependency>    <groupId>com.heliorm</groupId>     <artifactId>core</artifactId>     <version>0.91</version>  </dependency>

Annotations to annotate your POJOs

  <dependency>    <groupId>com.heliorm</groupId>     <artifactId>annotation</artifactId>     <version>0.9</version>  </dependency>

The MySQL driver

  <dependency>    <groupId>com.heliorm</groupId>     <artifactId>mysql</artifactId>     <version>0.9</version>  </dependency>

Quick CRUD examples

In these examples we have a POJO class calledDog and a running ORM referenced byorm.

ClassDog can more or less look like this:

@PojopublicclassDog {@PrimaryKey(autoIncrement =true)privateLongid;@ForeignKey(pojo =Person.class)privateLongpersonId;@Column(length =32)privateStringname;privateintage;}

Dog is annotated with@Pojo and some of it's fields are annotated as well. We'll explain more about annotationslater

Create

Create a new 3-year old dog, and call it 'Fido':

Dogdog =newDog();dog.setName("Fido");// The classics are the bestdog.setAge(3);dog.setPersonId(1);// code ommitted to set values in dogdog =orm.create(dog);

Read

Read the dog called 'Fido':

Dogdog =orm.select(DOG)   .where(DOG.name.eq("Fido"))   .one();

Update

Change a dog's name and save it:

// assume variable dog references a Dog object is loadeddog.setName("Rex");// I've renamed a dog IRL oncedog =orm.update(dog);

Delete

Delete a dog:

// assume variable dog references a Dog object is loadedorm.delete(dog);// Rip Rex

Retrieving data as lists, streams, singletons or optionals

Retrieve a list of data

Get all dogs in any order as a list:

List<Dogs>dogs =orm.select(DOG).list();

Get a single object

Get dog with ID 10 if you're sure it exists. If it doesn't exist, an exception will be thrown.

Dogdog =orm.select(DOG)                .where(DOG.id.eq(10L)                .one();

Get an optional object

Get dog with ID 10 if you're not sure it exists. If it doesn't exist, an emptyOptional is returned.

Optional<Dog>optDog =orm.select(DOG)                .where(DOG.id.eq(10L)                .optional();

Retrieve data as a stream

Get all dogs in any order as a stream:

Stream<Dogs>dogs =orm.select(DOG).stream();

Ordering

Order by sinlge column/field in ascending order

Get all dogs ordered by name:

List<Dog>alphaDogs =orm.select(DOG)   .orderBy(DOG.name)   .list();

Order by one column/field in descending order

Get all dogs ordered from oldest to youngest:

List<Dog>dogHierarchy =orm.select(DOG)   .orderBy(DOG.age.desc())   .list();

Order by a column/field in descending order, other in ascending

Get all dogs ordered from oldest to youngest:

List<Dog>dogHierarchy =orm.select(DOG)   .orderBy(DOG.age.desc(),DOG.name)   .list();

Joining data

Keep in mind whatDog looks like, and take a look atPerson:

@PojopublicclassPerson {@PrimaryKeyprivateLongid;@ForeignKey(pojo =Town.class)privateLongtownId;@ColumnprivateStringfirstName;@Column(nullable =true)privateStringlastName;@ColumnprivateStringemailAddress;@Column(nullable =true)privateDoubleincome;}

Select on a joined selection table

Get all dogs owned by Bob:

List<Dog>bobsDogs =orm.select(DOG)    .join(PERSON).on(DOG.personId,PERSON.id)    .where(PERSON.name.eq("Bob"))    .list();

Select on the result table and joined selection table

Get all dogs older than 2 years owned by Bob

List<Dog>bobsDogs =orm.select(DOG).where(DOG.age.gt(2))    .join(PERSON).on(DOG.personId,PERSON.id)    .where(PERSON.name.eq("Bob"))    .list();

About

A code-first object-relational model for accessing SQL data from Java

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp