在我的SQL Server 2000数据库中,我有一个DATETIME名为lastTouchedset 的类型的时间戳(在函数中不在数据类型中)列getdate()作为其默认值/ binding.
我正在使用Netbeans 6.5生成的JPA实体类,并在我的代码中使用它
@Basic(optional = false)
@Column(name = "LastTouched")
@Temporal(TemporalType.TIMESTAMP)
private Date lastTouched;
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将对象放入数据库时,我得到了,
javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.generic.Stuff.lastTouched
Run Code Online (Sandbox Code Playgroud)
我已经尝试设置设置@Basic为(optional = true),但是抛出一个异常,说数据库不允许列的null值TIMESTAMP,它不是设计的.
ERROR JDBCExceptionReporter - Cannot insert the value NULL into column 'LastTouched', table 'DatabaseName.dbo.Stuff'; column does not allow nulls. INSERT fails.
Run Code Online (Sandbox Code Playgroud)
我以前在纯Hibernate中使用它,但我有意识切换到JPA并且不知道如何告诉它该列被假设在数据库端生成.请注意,我仍然使用Hibernate作为我的JPA持久层.
我想知道一个类的一些成员变量的注释,我BeanInfo beanInfo = Introspector.getBeanInfo(User.class)用来内省一个类,并使用BeanInfo.getPropertyDescriptors(),找到特定的属性,并使用Class type = propertyDescriptor.getPropertyType()来获取属性的类.
但我不知道如何将注释添加到成员变量中?
我试过了type.getAnnotations(),type.getDeclaredAnnotations()但是,两者都返回了Class的注释,而不是我想要的.例如 :
class User
{
@Id
private Long id;
@Column(name="ADDRESS_ID")
private Address address;
// getters , setters
}
@Entity
@Table(name = "Address")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
class Address
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想得到地址的注释:@Column,而不是类地址的注释(@ Entity,@ Table,@ Cache).怎么实现呢?谢谢.
我只修改了spring boot配置,并遇到了
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
Run Code Online (Sandbox Code Playgroud)
从 org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration
@Bean(name = { "connect/twitterConnect", "connect/twitterConnected" })
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
public View twitterConnectView() {
return new GenericConnectionStatusView("twitter", "Twitter");
}
Run Code Online (Sandbox Code Playgroud)
我不明白这个注释的目的.我想这可能只有在存在属性值时才能使用bean(例如"spring.social","auto-connection-views").
我正在开发一个Java企业应用程序,目前正在使用Java EE安全性来限制特定用户对特定功能的访问.我配置了应用程序服务器和所有内容,现在我使用RolesAllowed-annotation来保护方法:
@Documented
@Retention (RUNTIME)
@Target({TYPE, METHOD})
public @interface RolesAllowed {
String[] value();
}
Run Code Online (Sandbox Code Playgroud)
当我使用这样的注释时,它工作正常:
@RolesAllowed("STUDENT")
public void update(User p) { ... }
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的,因为我必须在这里使用String,重构变得困难,并且可能发生拼写错误.因此,我想使用Enum值作为此注释的参数,而不是使用String.Enum看起来像这样:
public enum RoleType {
STUDENT("STUDENT"),
TEACHER("TEACHER"),
DEANERY("DEANERY");
private final String label;
private RoleType(String label) {
this.label = label;
}
public String toString() {
return this.label;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试使用Enum作为这样的参数:
@RolesAllowed(RoleType.DEANERY.name())
public void update(User p) { ... }
Run Code Online (Sandbox Code Playgroud)
但是后来我得到了以下编译器错误,虽然Enum.name只返回一个String(它总是不变的,不是吗?).
注释属性RolesAllowed.value的值必须是常量表达式`
我尝试的下一件事是在我的枚举中添加一个额外的最终字符串:
public enum RoleType {
...
public static final String STUDENT_ROLE = STUDENT.toString();
...
}
Run Code Online (Sandbox Code Playgroud)
但这也不能作为参数,导致相同的编译器错误:
// The …Run Code Online (Sandbox Code Playgroud) java.lang.annotation.ElementType:
程序元素类型.此枚举类型的常量提供Java程序中声明的元素的简单分类.这些常量与Target元注释类型一起使用,以指定使用注释类型的合法位置.
有以下常量:
有人可以解释它们中的每一个(在实际代码中它们会被注释)吗?
在基于Spring注释的控制器中,是否可以使用@RequestMapping不同的方法映射不同的查询字符串?
例如
@RequestMapping("/test.html?day=monday")
public void writeMonday() {
}
@RequestMapping("/test.html?day=tuesday")
public void writeTuesday() {
}
Run Code Online (Sandbox Code Playgroud) 我@SuppressWarnings在eclipse中为代码获取了注释的编译器警告:
@Override
public boolean doSomething(@SuppressWarnings("unused") String whatever) throws AnException {
throw new AnException("I'm still in bed and can't do anything until I've had a shower!");
}
Run Code Online (Sandbox Code Playgroud)
在"未使用"一词下看起来像一个黄色的波浪线,在鼠标悬停时,我得到了工具提示Unnecessary @SuppressWarnings("unused").
我认为另一个开发人员被提示通过eclipse输入这些注释,我基本上被提示将它们删除.我如何配置eclipse以提示我将@SuppressWarnings注释放入而不是抱怨它?
如果有人想在这里评论最佳实践,那么这也是最受欢迎的.
java eclipse annotations compiler-warnings suppress-warnings
我正在尝试将值的输出转换为整数:
@Value("${api.orders.pingFrequency}")
private Integer pingFrequency;
Run Code Online (Sandbox Code Playgroud)
以上抛出错误
org.springframework.beans.TypeMismatchException:
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer';
nested exception is java.lang.NumberFormatException:
For input string: "(java.lang.Integer)${api.orders.pingFrequency}"
Run Code Online (Sandbox Code Playgroud)
我也试过了 @Value("(java.lang.Integer)${api.orders.pingFrequency}")
谷歌似乎没有多说这个话题.我想总是处理一个整数,而不是必须在它使用的任何地方解析这个值.
解决方法
我意识到一个解决方法可能是使用setter方法为我运行转换,但如果Spring可以做到这一点,我宁愿学习一些关于Spring的东西.
我刚刚发现了这个功能.
使用"@interface"语法声明接口允许您设置默认值.
public @interface HelloWorld {
public String sayHello() default "hello world";
}
Run Code Online (Sandbox Code Playgroud)
这对我来说是新鲜事.如何使用该默认值.
我找不到对它的引用,因为在Java 1.5中添加"@"之前,www充满了java接口文档(是.5还是.4?)
编辑
谢谢你的答案(我在某种程度上接近"注释",因为我已经使用了标签):P
我知道多年前我应该读这份文件!!! ...让我们看看...
许多API需要相当数量的样板代码.对于....
Java 8引入了Lambda表达式和Type Annotations.
使用类型注释,可以定义Java注释,如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface MyTypeAnnotation {
public String value();
}
Run Code Online (Sandbox Code Playgroud)
然后可以在任何类型引用上使用此注释,例如:
Consumer<String> consumer = new @MyTypeAnnotation("Hello ") Consumer<String>() {
@Override
public void accept(String str) {
System.out.println(str);
}
};
Run Code Online (Sandbox Code Playgroud)
这是一个完整的例子,它使用这个注释来打印"Hello World":
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedType;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class Java8Example {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface MyTypeAnnotation {
public String value();
}
public static void main(String[] args) {
List<String> list = Arrays.asList("World!", "Type Annotations!"); …Run Code Online (Sandbox Code Playgroud) annotations ×10
java ×8
spring ×3
beaninfo ×1
casting ×1
eclipse ×1
enums ×1
java-8 ×1
java-ee ×1
java-ee-6 ×1
jpa ×1
lambda ×1
persistence ×1
reflection ×1
spring-boot ×1
timestamp ×1