本文整理汇总了Java中javax.persistence.Entity类的典型用法代码示例。如果您正苦于以下问题:Java Entity类的具体用法?Java Entity怎么用?Java Entity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Entity类属于javax.persistence包,在下文中一共展示了Entity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setupRelationshipRepositories
import javax.persistence.Entity; //导入依赖的package包/类
/**
* Sets up relationship repositories for the given document class. In case
* of a mapper the resource class might not correspond to the entity class.
*/
private void setupRelationshipRepositories(Class<?> resourceClass, boolean mapped) {
if (context.getResourceInformationBuilder().accept(resourceClass)) {
ResourceInformation information = context.getResourceInformationBuilder().build(resourceClass);
for (ResourceField field : information.getFields()) {
if (field.getResourceFieldType() != ResourceFieldType.RELATIONSHIP) {
continue;
}
Class<?> attrType = field.getElementType();
boolean isEntity = attrType.getAnnotation(Entity.class) != null;
if (isEntity) {
setupRelationshipRepositoryForEntity(resourceClass, field);
}
else {
setupRelationshipRepositoryForResource(resourceClass, field);
}
}
}
}
示例2: deleteEntities
import javax.persistence.Entity; //导入依赖的package包/类
public void deleteEntities(Class<?> entity){
if(entity == null || entity.getAnnotation(Entity.class) == null){
throw new IllegalArgumentException("Invalid non-entity class");
}
String name = entity.getSimpleName();
/*
Note: we passed as input a Class<?> instead of a String to
avoid SQL injection. However, being here just test code, it should
not be a problem. But, as a good habit, always be paranoiac about
security, above all when you have code that can delete the whole
database...
*/
Query query = em.createQuery("delete from " + name);
query.executeUpdate();
}
示例3: configureRepositoryRestConfiguration
import javax.persistence.Entity; //导入依赖的package包/类
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
//config.setBasePath("api");
try {
config.exposeIdsFor(UserEntity.class);
ClassPathUtils.streamClassesAnnotatedWith(UserEntity.class, Entity.class)
.peek(clazz -> log.debug("enable @Id json mapping for entity {}", clazz.getSimpleName()))
.forEach(config::exposeIdsFor);
} catch (IOException e) {
throw new IllegalStateException("Could not exposeIds for @Entity classes");
}
}
示例4: getClasses
import javax.persistence.Entity; //导入依赖的package包/类
/**
* Retourne les classes d'une package
*
* @param packageName
* @return
* @throws Exception
*/
public static List<Class> getClasses(String packageName) {
final List<Class> list = new ArrayList<>();
final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true);
scanner.addIncludeFilter(new AssignableTypeFilter(Object.class));
final Set<BeanDefinition> bds = scanner.findCandidateComponents(packageName);
try {
for (BeanDefinition bd : bds) {
final Class<?> tc = Class.forName(bd.getBeanClassName());
if (tc.getAnnotation(Entity.class) != null) {
list.add(tc);
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
return list;
}
示例5: weave
import javax.persistence.Entity; //导入依赖的package包/类
@Override
public void weave(WovenClass wovenClass) {
BundleWiring wiring = wovenClass.getBundleWiring();
Bundle bundle = wiring.getBundle();
ClassLoader cl = wiring.getClassLoader();
Collection<ClassTransformer> transformersToTry = getTransformers(bundle);
for (ClassTransformer transformer : transformersToTry) {
if (transformClass(wovenClass, cl, transformer)) {
LOGGER.info("Weaving " + wovenClass.getClassName() + " using " + transformer.getClass().getName());
break;
}
}
Class<?> dClass = wovenClass.getDefinedClass();
if (transformersToTry.isEmpty() && dClass != null && dClass.getAnnotation(Entity.class) != null) {
LOGGER.warn("Loading " + wovenClass.getClassName() + " before transformer is present");
}
}
示例6: deleteEntities
import javax.persistence.Entity; //导入依赖的package包/类
private void deleteEntities(Class<?> entity){
if(entity == null || entity.getAnnotation(Entity.class) == null){
throw new IllegalArgumentException("Invalid non-entity class");
}
String name = entity.getSimpleName();
/*
Note: we passed as input a Class<?> instead of a String to
avoid SQL injection. However, being here just test code, it should
not be a problem. But, as a good habit, always be paranoiac about
security, above all when you have code that can delete the whole
database...
*/
Query query = em.createQuery("delete from " + name);
query.executeUpdate();
}
示例7: addClassType
import javax.persistence.Entity; //导入依赖的package包/类
public AnnotatedClassType addClassType(XClass clazz) {
AnnotatedClassType type;
if ( clazz.isAnnotationPresent( Entity.class ) ) {
type = AnnotatedClassType.ENTITY;
}
else if ( clazz.isAnnotationPresent( Embeddable.class ) ) {
type = AnnotatedClassType.EMBEDDABLE;
}
else if ( clazz.isAnnotationPresent( javax.persistence.MappedSuperclass.class ) ) {
type = AnnotatedClassType.EMBEDDABLE_SUPERCLASS;
}
else {
type = AnnotatedClassType.NONE;
}
classTypes.put( clazz.getName(), type );
return type;
}
示例8: isEntityClassType
import javax.persistence.Entity; //导入依赖的package包/类
private static boolean isEntityClassType(XClass clazzToProcess, AnnotatedClassType classType) {
if ( AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals( classType ) //will be processed by their subentities
|| AnnotatedClassType.NONE.equals( classType ) //to be ignored
|| AnnotatedClassType.EMBEDDABLE.equals( classType ) //allow embeddable element declaration
) {
if ( AnnotatedClassType.NONE.equals( classType )
&& clazzToProcess.isAnnotationPresent( org.hibernate.annotations.Entity.class ) ) {
LOG.missingEntityAnnotation( clazzToProcess.getName() );
}
return false;
}
if ( !classType.equals( AnnotatedClassType.ENTITY ) ) {
throw new AnnotationException(
"Annotated class should have a @javax.persistence.Entity, @javax.persistence.Embeddable or @javax.persistence.EmbeddedSuperclass annotation: " + clazzToProcess
.getName()
);
}
return true;
}
示例9: buildHierarchyColumnOverride
import javax.persistence.Entity; //导入依赖的package包/类
private void buildHierarchyColumnOverride(XClass element) {
XClass current = element;
Map<String, Column[]> columnOverride = new HashMap<String, Column[]>();
Map<String, JoinColumn[]> joinColumnOverride = new HashMap<String, JoinColumn[]>();
Map<String, JoinTable> joinTableOverride = new HashMap<String, JoinTable>();
while ( current != null && !mappings.getReflectionManager().toXClass( Object.class ).equals( current ) ) {
if ( current.isAnnotationPresent( Entity.class ) || current.isAnnotationPresent( MappedSuperclass.class )
|| current.isAnnotationPresent( Embeddable.class ) ) {
//FIXME is embeddable override?
Map<String, Column[]> currentOverride = buildColumnOverride( current, getPath() );
Map<String, JoinColumn[]> currentJoinOverride = buildJoinColumnOverride( current, getPath() );
Map<String, JoinTable> currentJoinTableOverride = buildJoinTableOverride( current, getPath() );
currentOverride.putAll( columnOverride ); //subclasses have precedence over superclasses
currentJoinOverride.putAll( joinColumnOverride ); //subclasses have precedence over superclasses
currentJoinTableOverride.putAll( joinTableOverride ); //subclasses have precedence over superclasses
columnOverride = currentOverride;
joinColumnOverride = currentJoinOverride;
joinTableOverride = currentJoinTableOverride;
}
current = current.getSuperclass();
}
holderColumnOverride = columnOverride.size() > 0 ? columnOverride : null;
holderJoinColumnOverride = joinColumnOverride.size() > 0 ? joinColumnOverride : null;
holderJoinTableOverride = joinTableOverride.size() > 0 ? joinTableOverride : null;
}
示例10: getEntity
import javax.persistence.Entity; //导入依赖的package包/类
private Entity getEntity(Element tree, XMLContext.Default defaults) {
if ( tree == null ) {
return defaults.canUseJavaAnnotations() ? getPhysicalAnnotation( Entity.class ) : null;
}
else {
if ( "entity".equals( tree.getName() ) ) {
AnnotationDescriptor entity = new AnnotationDescriptor( Entity.class );
copyStringAttribute( entity, tree, "name", false );
if ( defaults.canUseJavaAnnotations()
&& StringHelper.isEmpty( (String) entity.valueOf( "name" ) ) ) {
Entity javaAnn = getPhysicalAnnotation( Entity.class );
if ( javaAnn != null ) {
entity.setValue( "name", javaAnn.name() );
}
}
return AnnotationFactory.create( entity );
}
else {
return null; //this is not an entity
}
}
}
示例11: build
import javax.persistence.Entity; //导入依赖的package包/类
public EntityManagerFactory build() {
Properties properties = createProperties();
DefaultPersistenceUnitInfoImpl persistenceUnitInfo = new DefaultPersistenceUnitInfoImpl(JSPARE_GATEWAY_DATASOURCE);
persistenceUnitInfo.setProperties(properties);
// Using RESOURCE_LOCAL for manage transactions on DAO side.
persistenceUnitInfo.setTransactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL);
// Add all entities to configuration
ClassAnnotationMatchProcessor processor = (c) -> persistenceUnitInfo.addAnnotatedClassName(c);
ClasspathScannerUtils.scanner(ALL_SCAN_QUOTE).matchClassesWithAnnotation(Entity.class, processor)
.scan(NUMBER_CLASSPATH_SCANNER_THREADS);
Map<String, Object> configuration = new HashMap<>();
properties.forEach((k, v) -> configuration.put((String) k, v));
EntityManagerFactory entityManagerFactory = persistenceProvider.createContainerEntityManagerFactory(persistenceUnitInfo,
configuration);
return entityManagerFactory;
}
示例12: start
import javax.persistence.Entity; //导入依赖的package包/类
@Override
public void start() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySetting("hibernate.connection.username", config.persistence.user)
.applySetting("hibernate.connection.password", config.persistence.pass)
.applySetting("hibernate.connection.driver_class", config.persistence.driver)
.applySetting("hibernate.connection.url", config.persistence.url)
.applySetting("hibernate.dialect", config.persistence.dialect)
.applySetting("hibernate.connection.pool_size", config.persistence.pool_size + "")
.applySetting("hibernate.hbm2ddl.auto", "update")
.applySetting("hibernate.show_sql", config.persistence.showSQL + "")
.build();
MetadataSources sources = new MetadataSources(registry);
Timings.time("RegisterDBEntities", () ->
new Reflections().getTypesAnnotatedWith(Entity.class).forEach(sources::addAnnotatedClass));
try {
Metadata metadata = sources.buildMetadata();
sessionFactory = metadata.buildSessionFactory();
} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
e.printStackTrace();
}
}
示例13: list
import javax.persistence.Entity; //导入依赖的package包/类
/**
*
* @param manager {@link EntityManager} to create the query
* @param klass klass to guess the list query and count query
* @param currentPage
* @param max max number of elements
* @return
*/
public <T> PaginatedList list(EntityManager manager, Class<T> klass,
int currentPage, int max)
{
if (!klass.isAnnotationPresent(Entity.class))
{
throw new IllegalArgumentException("Your entity is not annotated with @Entity");
}
TypedQuery<T> listQuery = manager.createQuery(
"select o from " + klass.getSimpleName() + " o", klass);
TypedQuery<Number> countQuery = manager.createQuery(
"select count(1) from " + klass.getSimpleName() + " o",
Number.class);
return list(listQuery, countQuery, currentPage, max);
}
示例14: getAllField
import javax.persistence.Entity; //导入依赖的package包/类
/**
*
* @param entityClass
* @param fieldList
* @return
*/
private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldList) {
if (fieldList == null) {
fieldList = new LinkedList<Field>();
}
if (entityClass.equals(Object.class)) {
return fieldList;
}
Field[] fields = entityClass.getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers())) {
fieldList.add(field);
}
}
Class<?> superClass = entityClass.getSuperclass();
if (superClass != null
&& !superClass.equals(Object.class)
&& (superClass.isAnnotationPresent(Entity.class)
|| (!Map.class.isAssignableFrom(superClass)
&& !Collection.class.isAssignableFrom(superClass)))) {
return getAllField(entityClass.getSuperclass(), fieldList);
}
return fieldList;
}
示例15: testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithoutInheritance
import javax.persistence.Entity; //导入依赖的package包/类
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithoutInheritance() throws Exception {
final String simpleClassName = "EntityClass";
final String nodeLabel = "ENTITY_CLASS";
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, simpleClassName);
jClass.annotate(Entity.class);
jClass.annotate(Table.class).param("name", nodeLabel);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> entityClass = loadClass(testFolder.getRoot(), jClass.name());
final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabel), Arrays.asList(entityClass));
assertThat(clazz, equalTo(entityClass));
}