Java Code Examples for javax.persistence.GeneratedValue

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project Core_2, under directory /javaee-impl/src/main/java/org/jboss/forge/spec/javaee/jpa/.

Source file: EntityPlugin.java

  15 
vote

@SuppressWarnings("unchecked") @DefaultCommand(help="Create a JPA @Entity") public void newEntity(@Option(required=true,name="named",description="The @Entity name") final String entityName,@Option(required=false,name="package",type=PromptType.JAVA_PACKAGE,description="The package name") final String packageName,@Option(name="idStrategy",defaultValue="AUTO",description="The GenerationType name for the ID") final GenerationType idStrategy) throws Throwable {
  final PersistenceFacet jpa=project.getFacet(PersistenceFacet.class);
  final JavaSourceFacet java=project.getFacet(JavaSourceFacet.class);
  String entityPackage;
  if ((packageName != null) && !"".equals(packageName)) {
    entityPackage=packageName;
  }
 else   if (getPackagePortionOfCurrentDirectory() != null) {
    entityPackage=getPackagePortionOfCurrentDirectory();
  }
 else {
    entityPackage=shell.promptCommon("In which package you'd like to create this @Entity, or enter for default",PromptType.JAVA_PACKAGE,jpa.getEntityPackage());
  }
  JavaClass javaClass=JavaParser.create(JavaClass.class).setPackage(entityPackage).setName(entityName).setPublic().addAnnotation(Entity.class).getOrigin().addInterface(Serializable.class);
  Field<JavaClass> id=javaClass.addField("private Long id = null;");
  id.addAnnotation(Id.class);
  id.addAnnotation(GeneratedValue.class).setEnumValue("strategy",idStrategy);
  id.addAnnotation(Column.class).setStringValue("name","id").setLiteralValue("updatable","false").setLiteralValue("nullable","false");
  Field<JavaClass> version=javaClass.addField("private int version = 0;");
  version.addAnnotation(Version.class);
  version.addAnnotation(Column.class).setStringValue("name","version");
  Refactory.createGetterAndSetter(javaClass,id);
  Refactory.createGetterAndSetter(javaClass,version);
  Refactory.createToStringFromFields(javaClass,id);
  Refactory.createHashCodeAndEquals(javaClass);
  JavaResource javaFileLocation=java.saveJavaSource(javaClass);
  shell.println("Created @Entity [" + javaClass.getQualifiedName() + "]");
  shell.execute("pick-up " + javaFileLocation.getFullyQualifiedName().replace(" ","\\ "));
}
 

Example 2

From project Core_2, under directory /javaee-impl/src/test/java/org/jboss/forge/spec/jpa/.

Source file: NewEntityPluginTest.java

  15 
vote

@Test public void testNewEntityIdStrategy() throws Exception {
  Project project=getProject();
  String entityName="Goofy";
  queueInputLines("");
  getShell().execute(ConstraintInspector.getName(EntityPlugin.class) + " --named " + entityName+ " --idStrategy TABLE");
  String pkg=project.getFacet(PersistenceFacet.class).getEntityPackage() + "." + entityName;
  String path=Packages.toFileSyntax(pkg) + ".java";
  JavaClass javaClass=(JavaClass)project.getFacet(JavaSourceFacet.class).getJavaResource(path).getJavaSource();
  assertFalse(javaClass.hasSyntaxErrors());
  javaClass=(JavaClass)project.getFacet(JavaSourceFacet.class).getJavaResource(javaClass).getJavaSource();
  assertFalse(javaClass.hasSyntaxErrors());
  assertTrue(javaClass.hasImport(Entity.class));
  assertTrue(javaClass.hasField("id"));
  Field<JavaClass> idField=javaClass.getField("id");
  assertTrue(idField.hasAnnotation(GeneratedValue.class));
  Annotation<JavaClass> genValueAnn=idField.getAnnotation(GeneratedValue.class);
  assertEquals(GenerationType.TABLE,genValueAnn.getEnumValue(GenerationType.class,"strategy"));
}
 

Example 3

From project Core_2, under directory /scaffold-faces/src/test/java/org/jboss/forge/scaffold/faces/.

Source file: PrimaryKeyFacesScaffoldTest.java

  15 
vote

@SuppressWarnings("unchecked") private void generateLegacyEntities(Project project) throws FileNotFoundException {
  JavaSourceFacet java=project.getFacet(JavaSourceFacet.class);
  JavaClass javaClass=JavaParser.create(JavaClass.class).setPackage("com.test.model").setName("State").setPublic().addAnnotation(Entity.class).getOrigin().addInterface(Serializable.class);
  String idName="stateId";
  Field<JavaClass> id=javaClass.addField("private String " + idName + " = null;");
  id.addAnnotation(Id.class);
  id.addAnnotation(GeneratedValue.class).setEnumValue("strategy",GenerationType.AUTO);
  Refactory.createGetterAndSetter(javaClass,id);
  Refactory.createToStringFromFields(javaClass,id);
  Refactory.createHashCodeAndEquals(javaClass);
  java.saveJavaSource(javaClass);
  javaClass=JavaParser.create(JavaClass.class).setPackage("com.test.model").setName("Parent").setPublic().addAnnotation(Entity.class).getOrigin().addInterface(Serializable.class);
  idName="parentId";
  id=javaClass.addField("private String " + idName + " = null;");
  id.addAnnotation(Id.class);
  id.addAnnotation(GeneratedValue.class).setEnumValue("strategy",GenerationType.AUTO);
  id.addAnnotation(Column.class).setStringValue("name",idName).setLiteralValue("updatable","false").setLiteralValue("nullable","false");
  Refactory.createGetterAndSetter(javaClass,id);
  Field<JavaClass> name=javaClass.addField("private String name = null;");
  name.addAnnotation(Column.class).setLiteralValue("nullable","false");
  Refactory.createGetterAndSetter(javaClass,name);
  Field<JavaClass> state=javaClass.addField("private State state = null;");
  state.addAnnotation(ManyToOne.class).setLiteralValue("optional","false");
  Refactory.createGetterAndSetter(javaClass,state);
  Refactory.createToStringFromFields(javaClass,id);
  Refactory.createHashCodeAndEquals(javaClass);
  java.saveJavaSource(javaClass);
}
 

Example 4

From project oreilly-ejb-6thedition-book-examples, under directory /ch09-14-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/.

Source file: EmployeeIntegrationTest.java

  15 
vote

/** 
 * Shows usage of JPA autogeneration of primary keys, using  {@link EmployeeWithMappedSuperClassId} which inherits PK support from{@link IdentityBase#getId()}.
 * @throws Throwable
 */
@Test public void autogenPrimaryKeyFromMappedSuperClass() throws Throwable {
  try {
    final Long id=txWrapper.wrapInTx(new Callable<Long>(){
      @Override public Long call() throws Exception {
        final EmployeeWithMappedSuperClassId alrubinger=new EmployeeWithMappedSuperClassId("Andrew Lee Rubinger");
        Assert.assertNull("Primary key should not be set yet",alrubinger.getId());
        emHook.getEntityManager().persist(alrubinger);
        final Long id=alrubinger.getId();
        Assert.assertNotNull("Persisting an entity with PK " + GeneratedValue.class.getName() + " should be created",id);
        log.info("Persisted: " + alrubinger);
        return id;
      }
    }
);
    txWrapper.wrapInTx(new Callable<Void>(){
      @Override public Void call() throws Exception {
        final EmployeeWithMappedSuperClassId employee=emHook.getEntityManager().find(EmployeeWithMappedSuperClassId.class,id);
        Assert.assertNotNull("Employee should be able to be looked up by PK",employee);
        return null;
      }
    }
);
  }
 catch (  final TaskExecutionException tee) {
    throw tee.getCause();
  }
}