ORMLite - 1.0
Extension ID
com.castsoftware.ormlite
What’s new?
See ORMLite - 1.0 - Release Notes for more information.
Description
This extension provides support for ORMLite framework for interactions between Java code and RDBMS via CRUD operations.
Note that com.castsoftware.jee provides full support for JPA and Hibernate, however, this extension provides additional support and should be used for any application using ORMLite libraries.
In what situation should you install this extension?
If your Java application uses a supported ORMLite framework and you want to view these object types and their links, then you should install this extension. More specifically the extension will identify:
- “callLinks” from Java methods to ORMLite Entity Operation objects.
- “callLinks” from Java methods to ORMLiteSQL Query objects.
Technology support
Library name | Version | Supported | Supported Technology |
---|---|---|---|
ORMLite | 1.0 to 6.1 | Java |
Supported persistence libraries
Supported operations
Operation | Methods Supported |
---|---|
Select |
|
Insert |
|
Update |
|
Delete |
|
Query Based |
|
Supported ORMLite Annotations
- DatabaseTable
Compatibility
This extension is compatible with:
CAST Imaging Core release | Supported |
---|---|
≥ 8.3.x |
Download and installation instructions
For Java applications using any of the above mentioned libraries, this extension will be automatically installed by CAST Imaging Console. This is in place since October 2023. For upgrade, if the Extension Strategy is not set to Auto update, you can manually upgrade the extension using the Application - Extensions interface.
What results can you expect?
Once the analysis/snapshot generation is completed, you can view the results. The following objects and links will be displayed:
Objects
Icon | Description | Comment |
---|---|---|
ORMLite Entity | an object is created when @DatabaseTable annotation is encountered | |
ORMLite Entity Operation | an object is created for each CRUD operation performed on Entity | |
ORMLite SQL Query | an object is created for each native SQL query found and resolved in a method call |
Links
Link Type |
Caller type | Callee type |
Supported APIs |
---|---|---|---|
callLink
|
Java Method | ORMLite Entity Operation |
|
callLink | Java Method | callLink between the caller Java Method and ORMLite Query object |
|
useLink | ORMLite Entity Operation | Table / View | Created by com.castsoftware.wbs when DDL source files are analyzed by SQL Analyzer |
Code examples
CRUD Operations
ORMLite Entity
package com.spring.ormlite.entities;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "record")
public class GeneralRecord {
@DatabaseField(id = true)
int id;
@DatabaseField
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public GeneralRecord(int id, String name) {
super();
this.id = id;
this.name = name;
}
public GeneralRecord() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "GeneralRecord [id=" + id + ", name=" + name + "]";
}
}
Add operation
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.jdbc.db.MysqlDatabaseType;
import com.j256.ormlite.stmt.*;
import com.j256.ormlite.support.ConnectionSource;
public class generateGeneralApp{
public static void generateGeneralRecord() throws Exception {
String databaseUrl = "jdbc:mysql://localhost:3306/ormlite";
//String databaseUrl = "jdbc:h2:~/test";
ConnectionSource connectionSource =
new JdbcConnectionSource(databaseUrl,"root","root",new MysqlDatabaseType());
System.out.println("Connection successfull");
Dao<GeneralRecord,String> recordDao =
DaoManager.createDao(connectionSource, GeneralRecord.class);
TableUtils.createTableIfNotExists(connectionSource, GeneralRecord.class);
int name = 2;
GeneralRecord record = new GeneralRecord(name, "record "+name);
recordDao.create(record);
connectionSource.close();
}
}
Add operation through android application
package com.example.helloandroid;
import java.util.List;
import java.util.Random;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.RuntimeExceptionDao;
/**
* Sample Android UI activity which displays a text window when it is run.
*/
public class HelloAndroid extends OrmLiteBaseActivity<DatabaseHelper> {
private final String LOG_TAG = getClass().getSimpleName();
private final static int MAX_NUM_TO_CREATE = 8;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(LOG_TAG, "creating " + getClass() + " at " + System.currentTimeMillis());
TextView tv = new TextView(this);
tv.setMovementMethod(new ScrollingMovementMethod());
doSampleDatabaseStuff("onCreate", tv);
setContentView(tv);
}
/**
* Do our sample database stuff.
*/
private void doSampleDatabaseStuff(String action, TextView tv) {
// get our dao
RuntimeExceptionDao<SimpleData, Integer> simpleDao = getHelper().getSimpleDataDao();
// query for all of the data objects in the database
List<SimpleData> list = simpleDao.queryForAll();
// our string builder for building the content-view
StringBuilder sb = new StringBuilder();
sb.append("Found ").append(list.size()).append(" entries in DB in ").append(action).append("()\n");
int createNum;
do {
createNum = new Random().nextInt(MAX_NUM_TO_CREATE) + 1;
} while (createNum == list.size());
sb.append("Creating ").append(createNum).append(" new entries:\n");
for (int i = 0; i < createNum; i++) {
long millis = System.currentTimeMillis();
SimpleData simple = new SimpleData(millis);
simpleDao.create(simple);
Log.i(LOG_TAG, "created simple(" + millis + ")");
}
}
}
Update Operation
public static void updateSchoolData() throws SQLException {
String databaseUrl = "jdbc:mysql://localhost:3306/ormlite";
ConnectionSource connectionSource =
new JdbcConnectionSource(databaseUrl,"root","root",new MysqlDatabaseType());
Dao<School,String> schoolDao =
DaoManager.createDao(connectionSource, School.class);
School s = new School("5","sunbeam");
schoolDao.updateId(s,"8");
}
Delete operation
public static void deleteSchoolData() throws SQLException {
String databaseUrl = "jdbc:mysql://localhost:3306/ormlite";
ConnectionSource connectionSource =
new JdbcConnectionSource(databaseUrl,"root","root",new MysqlDatabaseType());
Dao<School,String> schoolDao =
DaoManager.createDao(connectionSource, School.class);
schoolDao.deleteById("8");
}
Select operation
public class AccountApp {
public static void main(String[] args) throws Exception {
String databaseUrl = "jdbc:mysql://localhost:3306/ormlite";
ConnectionSource connectionSource =
new JdbcConnectionSource(databaseUrl,"root","root",new MysqlDatabaseType());
Dao<Account,String> accountDao =
DaoManager.createDao(connectionSource, Account.class);
querywithWhereSchool(accountDao);
connectionSource.close();
}
public static void querywithWhereSchool(Dao<Account,String> accountDao) throws SQLException {
Account s=new Account("10","ava");
String id = accountDao.extractId(s);
System.out.println("account id "+id);
}
}
ORMLite Query
Raw SQL Query
public static void rawquery() throws SQLException {
String query = "select * from libraries where libraryId = 1";
rawSqlQuery(query);
}
public static void rawSqlQuery(String query) throws SQLException {
String databaseUrl = "jdbc:mysql://localhost:3306/ormlite";
ConnectionSource connectionSource =
new JdbcConnectionSource(databaseUrl,"root","root",new H2DatabaseType());
Dao<Libraries,String> accountDao =
DaoManager.createDao(connectionSource, Libraries.class);
GenericRawResults<String[]> rawResults =
accountDao.queryRaw(query);
// there should be 1 result
List<String[]> results = rawResults.getResults();
// the results array should have 1 value
String[] resultArray = results.get(0);
// this should print the number of orders that have this account-id
System.out.println("id " + resultArray[0] + " value " + resultArray[1]);
}
Custom SQL Query
public class UserApp {
public static void main(String[] args) throws Exception {
String databaseUrl = "jdbc:mysql://localhost:3306/ormlite";
ConnectionSource connectionSource =
new JdbcConnectionSource(databaseUrl,"root","root",new MysqlDatabaseType());
Dao<User,String> userDao =
DaoManager.createDao(connectionSource, User.class);
queryUser(userDao);
connectionSource.close();
}
public static void queryUser(Dao<User, String> schoolDao) throws SQLException {
QueryBuilder<User, String> queryBuilder =
schoolDao.queryBuilder();
queryBuilder.selectColumns(User.name_FIELD_NAME);
queryBuilder.groupBy(User.name_FIELD_NAME);
queryBuilder.orderBy(User.name_FIELD_NAME,false);
queryBuilder.limit(1L);
List<User> accountList = queryBuilder.query();
}
Limitation
- Objects will not be created if the evaluation fails to resolve the necessary parameters.