Skip to content

Instantly share code, notes, and snippets.

@sinergy
Created January 21, 2015 07:20
Show Gist options
  • Save sinergy/bc3a09e4604f89544740 to your computer and use it in GitHub Desktop.
Save sinergy/bc3a09e4604f89544740 to your computer and use it in GitHub Desktop.
DaoGenerator snippet created for my blog post.
package com.cloudchen;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Schema;
public class MyDaoGenerator {
private static final String GENERATED_SRC_DIR = "./app/gen-src";
public static void main(String[] args) throws Exception {
Schema schema = new Schema(1, MyDaoGenerator.class.getPackage().getName());
Entity entity = schema.addEntity("User");
entity.addLongProperty("UserId").notNull();
entity.addStringProperty("Password").notNull();
entity.addStringProperty("Name").notNull();
// need to create the folder by ourself, or the generateAll() method will cause I/O error
createDirs(Paths.get(GENERATED_SRC_DIR));
new DaoGenerator().generateAll(schema, GENERATED_SRC_DIR);
}
private static void createDirs(Path path) throws IOException {
if (Files.notExists(path, LinkOption.NOFOLLOW_LINKS)) {
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms);
Files.createDirectories(path, attrs);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment