Tag: 注释

关于JPA级联的问题

我的datamodel中有两个名为User和UserProfile的实体。 以下是它们的映射方式。 用户实体的代码: @OneToOne(cascade=CascadeType.ALL) @PrimaryKeyJoinColumn public UserProfile getUserProfile(){ return this.userProfile; } public void setUserProfile(UserProfile userProfile){ this.userProfile=userProfile; } 来自UserProfile实体的代码: @OneToOne(mappedBy=”userProfile”,cascade=CascadeType.ALL) public User getUser(){ return this.user; } public void setUser(User user){ this.user=user; } 如您所见,我在UserProfile中有一个用于user属性的cascadetype.all。 但是当我尝试删除UserProfile实体时,相应的用户实体仍然存在。 (当我尝试删除User实体时,相应的UserProfile实体被删除。) 这是我的问题: – 只有当我在拥有这种关系的实体上指定级联时才进行级联吗?

检查注释是否属于特定类型

我正在使用reflection来查看附加到类的属性的注释是否是特定类型。 目前我在做: if(“javax.validation.Valid”.equals(annotation.annotationType().getName())) { … } 这让我觉得有点笨拙,因为它依赖于一个完全限定的类名字符串。 如果命名空间将来发生更改,则可能会导致细微错误。 我想要做: if(Class.forName(annotation.annotationType().getName()).isInstance( new javax.validation.Valid() )) { … } 但是javax.validation.Valid是一个抽象类,无法实例化。 有没有办法模拟针对接口或抽象类的instanceof (或基本上使用isInstance )?

如何在java中创建自定义注释?

我想在Java中为DirtyChecking创建自定义注释。 就像我想使用此注释比较两个字符串值,并在比较后将返回一个boolean值。 例如:我将@DirtyCheck(“newValue”,”oldValue”)放在属性上。 假设我创建了一个界面: public @interface DirtyCheck { String newValue(); String oldValue(); } 我的问题是 : 我在哪里创建一个类来创建一个比较两个字符串值的方法? 我的意思是,这个注释如何通知我必须调用这个方法? 如何检索此方法的返回值?

在注释处理器中获取字段类

我正在编写我的第一个Annotations处理器,并且遇到一些看似微不足道的问题,但我找不到任何有关它的信息。 我有一个用我的注释注释的元素 @MyAnnotation String property; 当我将此属性作为处理器中的元素时,我似乎无法以任何方式获取元素的类型。 在这种情况下,a希望获得表示String的Class或TypeElement实例。 我尝试使用Class.forName()实例化容器类型的类对象,但它抛出了ClassNotFoundException。 我想这是因为我无法访问包含该类的类加载器?

创建自定义AbstractProcessor并与Eclipse集成

我正在尝试创建一个新的注释,我将在其中进行一些运行时布线,但是,由于多种原因,我想在编译时validation我的布线是否会成功进行一些基本检查。 假设我创建了一个新注释: @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface CustomAnnotation{ } 现在我想在编译时进行某种validation,比如检查CustomAnnotation注释的字段是否为特定类型: ParticularType 。 我在Java 6中工作,所以我创建了一个AbstractProcessor : @SupportedAnnotationTypes(“com.example.CustomAnnotation”) public class CompileTimeAnnotationProcessor extends AbstractProcessor { @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { Set elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class); for(Element e : elements){ if(!e.getClass().equals(ParticularType.class)){ processingEnv.getMessager().printMessage(Kind.ERROR, “@CustomAnnotation annotated fields must be of type ParticularType”); } } return true; } } 然后,根据我发现的一些指令,我创建了一个文件夹META-INF/services并创建了一个文件javax.annotation.processing.Processor其中包含以下内容: com.example.CompileTimeAnnotationProcessor […]

查找带注释的注释

有一个注释@MarkerAnnotation 。 它可以直接添加到方法中。 或任何其他注释,如 @MarkerAnnotation @Interface CustomAnnotation {…} 这个@CustomAnnotation也可以直接添加到方法中。 这是许多框架允许用户添加自己的注释(例如spring)的标准方式。 现在,给定一个类,我想找到所有用@MarkerAnnotation直接或间接标记的方法。 对于每个方法,我还想找到相关的@MarkerAnnotation和/或@CustomAnnotation 。 有没有我可以使用的工具,或者我必须手动完成它?

在Annotations中访问属性值

我想在我的注释中访问属性值,作为属性的值。 对于前者 在我的属性文件中,我有一个条目表达式: 3/10 * * * * ? 。 在我的Scheduler类中,我使用注释@Scheduled (cron = “**VALUE**”) 。 我想从与表达式键对应的属性文件中读取此值。 尝试使用@Value执行此@Value ,但它返回一个无法转换为String的Value类型。

ORMLite错误没有存在databasefield注释

运行我的Android应用程序时出现此错误: 没有字段在类[[Lmodel.Vak;]中有一个DatabaseField注释。 我的class级Vak有注释,所以我真的不明白为什么它仍然给我这个错误。 package model; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName = “vak”) public class Vak { @DatabaseField(generatedId = true, columnName = “vakID”, id=true) Long id; @DatabaseField int rij; @DatabaseField int kolom; … } 我有一个名为Databasehelper.java的文件,其中扩展了OrmLiteSqLiteOpenHelper,文件如下所示: public class DatabaseHelper extends OrmLiteSqliteOpenHelper { // name of the database file for your application — change to something // appropriate […]

如何访问注释属性中描述的字段

是否可以访问字段值,其中字段名称在注释中描述,该注释注释了类中的另一个字段。 例如: @Entity public class User { @NotBlank private String password; @Match(field = “password”) private String passwordConfirmation; } 注解: @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = FieldMatchValidator.class) @Documented public @interface Match { String message() default “{}”; Class[] groups() default {}; Class[] payload() default {}; String field(); } 现在,是否可以从ConstraintValidator实现类中的类User访问字段密码? 编辑: 我写了这样的话: public class MatchValidator implements ConstraintValidator { private String […]

无法获得在Spring Security中使用的@Secured Method Security注释

我已经做了很多研究,对我而言,一切看起来都很正确……但我无法让它发挥作用! 任何人都有任何想法? 无论我做什么,相关的映射仍然公开给任何人(匿名或登录,无论他们有什么角色)。 理想情况下,我希望所有请求都是公共的,除了那些由@Secured()注释的请求 – 显然只有具有特定角色的用户才能访问这些映射。 那可能吗? 仅供参考我作为一种解决方法我目前构建了一个方法“hasRole(String role)”,它检查登录用户的角色,如果方法返回false,则抛出NotAuthorizedException(自定义)。 的UserDetails @Override public Collection getAuthorities() { List grantedAuthorities = null; System.out.print(“Account role… “); System.out.println(account.getRole()); if (account.getRole().equals(“USER”)) { GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(“ROLE_USER”); grantedAuthorities = Arrays.asList(grantedAuthority); } if (account.getRole().equals(“ADMIN”)) { GrantedAuthority grantedAuthorityUser = new SimpleGrantedAuthority(“ROLE_USER”); GrantedAuthority grantedAuthorityAdmin = new SimpleGrantedAuthority(“ROLE_ADMIN”); grantedAuthorities = Arrays.asList(grantedAuthorityUser, grantedAuthorityAdmin); } return grantedAuthorities; […]