devloop icon
Metastores

Custom mapping tutorial

This tutorial shows you examples of greater control over the mapping process

Here we use a large set of annotations to show some of the possibilities offered by the mapping process.

	@TableName("Three")
	public class Three {
		@ColumnName("id")	@PrimaryKey
		public	int	theKey;
		@Unique
		public	String	name;
		@Ignored
		public	String	notMadePersistent;
		@Indexed
		public	String	lookup;
		@Required
		public	String	required;
		@MaxLength(10)
		public	String	code;
		@OrderBy(true)
		public	short	order;
	}
download source
The TableName and ColumnName are explained in the reverse mapping tutorial, all the other annotations should be self explanatory, see below for the effect they have on the schema.

We then create a simple program to access these records,

	DBDataMapper dbm = new DBDataMapper();
	dbm.checkApplication(Three.class);
	dbm.createSchema();
	Three three = new Three();
	three.required = "somestring";
	three.code = "1234";
	three.name = "name1";
	three.order = 1;
	IOHelper.insert(three);
	three.name = "name2";
	three.code = "0001";
	three.order = 0;
	IOHelper.insert(three);
	//Read all of them:
	for (Three t : IOHelper.readAll(Three.class)) {
		System.out.println(t.code+" "+t.name+" "+t.order+" "+t.theKey);
	}
	//Read one by name
	Three name1 = IOHelper.readObject(Three.class, "name", "name2");
	System.out.println(name1.code+" "+name1.name+" "+name1.order+" "+name1.theKey);
	
	dbm.dropSchema();
   
download source

Running this program produces the following output (sanitized):

	CREATE  TABLE Three (
		   id INT NOT NULL PRIMARY KEY , 
		   name VARCHAR(255) UNIQUE,
		   lookup VARCHAR(255),
		   required VARCHAR(255) NOT NULL,
		   code VARCHAR(10),
		   D_order INT,
		   INDEX (lookup))
	SELECT MAX(id) FROM Three;
	INSERT INTO Three(id,name,lookup,required,code,D_order) VALUES (?,?,?,?,?,?)
	INSERT INTO Three(id,name,lookup,required,code,D_order) VALUES (?,?,?,?,?,?)
	SELECT id,name,lookup,required,code,D_order FROM Three ORDER BY D_order ASC
	0001 name2 0 2
	1234 name1 1 1
	SELECT id,name,lookup,required,code,D_order FROM Three WHERE name=? ORDER BY D_order ASC
	0001 name2 0 2
	ALTER TABLE Three DROP INDEX lookup
	DROP TABLE Three
Notes:

Related Links