标签: annotations

@OneToMany mappedBy映射到_____

什么mappedBy地图?或者,相反,它应该映射到什么?

headers下面的字段@Entity Foo按照@OneToMany 文档进行映射?然后Foo将是一个包装javax.mail.Header

package net.bounceme.dur.usenet.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.persistence.*;

@Entity
public class Articles implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(Articles.class.getName());
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column
    private String subject;

    @OneToMany(mappedBy="foo")  //a wrapper for Header is needed?
    private List<Header> headers = …
Run Code Online (Sandbox Code Playgroud)

java annotations jpa one-to-many jpql

30
推荐指数
1
解决办法
5万
查看次数

Spring MVC和Servlets 3.0 - 你还需要web.xml吗?

在典型的Spring MVC Web应用程序中,您可以像这样声明DispatcherServletinweb.xml

<!-- MVC Servlet -->
<servlet>
    <servlet-name>sample</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>sample</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

与听众,过滤器等一起

使用servlet-api 3.0,您可以使用注释声明servlet,@WebServlet而不是将它们添加到您的web.xml.春季3.2已经@Configuration@EnableXYZ它的上下文配置.是否有类似的东西DispatcherServlet,即.有没有办法配置你的完整的Spring应用程序没有任何xml?

java annotations spring-mvc

30
推荐指数
1
解决办法
2万
查看次数

@Javax.persistence.Lob注释在JPA中有什么意义?

@javax.persistence.Lob什么时候应该在JPA中使用注释?此注释可以注释哪些数据类型?

java annotations jpa java-ee-7

30
推荐指数
2
解决办法
2万
查看次数

Hibernate注释中的继承?

如何使用Java注释配置Hibernate继承映射?在Annotations中使用继承有什么好处?

inheritance annotations hibernate

29
推荐指数
2
解决办法
4万
查看次数

@OneToMany和复合主键?

我正在使用带注释的Hibernate(在spring中),并且我有一个对象,它有一个有序的,多对一的关系,一个子对象有一个复合主键,其中一个组件是一个外键回到父对象的id.

结构看起来像这样:

+=============+                 +================+
| ParentObj   |                 | ObjectChild    |
+-------------+ 1          0..* +----------------+
| id (pk)     |-----------------| parentId       |
| ...         |                 | name           |
+=============+                 | pos            |
                                | ...            |
                                +================+
Run Code Online (Sandbox Code Playgroud)

我尝试了各种注释组合,但似乎都没有.这是我能够提出的最接近的:

@Entity
public class ParentObject {
    @Column(nullable=false, updatable=false)
    @Id @GeneratedValue(generator="...")
    private String id;

    @OneToMany(mappedBy="parent", fetch=FetchType.EAGER, cascade={CascadeType.ALL})
    @IndexColumn(name = "pos", base=0)
    private List<ObjectChild> attrs;

    ...
}

@Entity
public class ChildObject {
    @Embeddable
    public static class Pk implements Serializable {
        @Column(nullable=false, updatable=false)
        private String parentId;

        @Column(nullable=false, updatable=false)
        private …
Run Code Online (Sandbox Code Playgroud)

java annotations hibernate composite-key one-to-many

29
推荐指数
3
解决办法
6万
查看次数

如何通过ID注入Spring依赖项?

我有几个相同类型的bean(BeanType).如何通过带注释的ID注入它们?说:

@Autowired @ID("bean1")
public void setBean( BeanType bean ) {
}
Run Code Online (Sandbox Code Playgroud)

但是没有注释@ID.

我只发现@Qualifier这意味着我必须提供所有的bean ID 限定符.当然,还有一种更简单的方法吗?

spring annotations dependency-injection

29
推荐指数
1
解决办法
4万
查看次数

将注释属性传递给元注释

假设我有一个属性的注释:

@Named(name = "Steve")
private Person person
Run Code Online (Sandbox Code Playgroud)

我想创建一个带有多个元注释的复合注释,包括带有属性的元注释

@Named
@AnotherAnnotation
@YetAnotherAnnotation
public @interface CompoundAnnotation {

    ...
}
Run Code Online (Sandbox Code Playgroud)

有没有办法可以将属性传递给复合注释到其中一个元注释?

例如,像这样:

@CompoundAnnotation(name = "Bob")
private Person person;
Run Code Online (Sandbox Code Playgroud)

这相当于,但比方便得多

@Named(name = "Bob")
@AnotherAnnotation
@YetAnotherAnnotation
private Person person;
Run Code Online (Sandbox Code Playgroud)

谢谢!

PS为我糟糕的示例注释选择道歉 - 我没有javax.inject.@ Named注释,只是一些具有属性的任意注释.


谢谢大家的回答/评论.

这似乎是不可能的.然而,恰好有一个简单的解决方案,我将分享以下情况,如果它可以帮助任何人:

我正在使用Spring,并希望创建自己的注释,将@Component作为元注释,从而通过组件扫描自动检测.但是,我还希望能够设置BeanName属性(对应于@Component中的value属性),这样我就可以拥有自定义bean名称.

好吧,事实证明Spring的有思想的人可以做到这一点 - AnnotationBeanNameGenerator将采用它传递的任何注释的'value'属性并将其用作bean名称(当然,默认情况下,它将只传递@Component的注释或将@Component作为元注释.回想起来,这应该从一开始就很明显 - 这就是使用@Component作为元注释的现有注释,例如@Service和@Registry,可以提供bean名称.

希望对某人有用.尽管如此,我仍然认为这是不可能的,这是一种耻辱!

java annotations

29
推荐指数
2
解决办法
5343
查看次数

如何使用自定义注释@Foo查找所有bean?

我有这个弹簧配置:

@Lazy
@Configuration
public class MyAppConfig {
    @Foo @Bean
    public IFooService service1() { return new SpecialFooServiceImpl(); }
}
Run Code Online (Sandbox Code Playgroud)

如何获取所有注释的bean列表@Foo

注意:@Foo是我定义的自定义注释.这不是"官方"Spring注释之一.

[编辑]根据Avinash T.的建议,我写了这个测试用例:

import static org.junit.Assert.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import java.lang.annotation.Retention;
import java.lang.reflect.Method;
import java.util.Map;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

public class CustomAnnotationsTest {

    @Test
    public void testFindByAnnotation() throws Exception {

        AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext( CustomAnnotationsSpringCfg.class );

        Method m = CustomAnnotationsSpringCfg.class.getMethod( "a" );
        assertNotNull( m …
Run Code Online (Sandbox Code Playgroud)

configuration spring annotations

29
推荐指数
4
解决办法
4万
查看次数

如何扩展Java注释?

在我的项目中,我使用预定义的注释@With:

@With(Secure.class)
public class Test { //....
Run Code Online (Sandbox Code Playgroud)

源代码@With:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface With { 

    Class<?>[] value() default {};
}
Run Code Online (Sandbox Code Playgroud)

我想编写自定义注释@Secure,其效果与之相同@With(Secure.class).怎么做?


如果我这样喜欢怎么办?它会起作用吗?

@With(Secure.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {

}
Run Code Online (Sandbox Code Playgroud)

java annotations

29
推荐指数
4
解决办法
4万
查看次数

通过注释而不是XML配置Spring LdapTemplate的最佳实践?

对于Spring Boot应用程序,我LdapTemplate使用注释成功配置了Spring ,包括LdapContextSource@Valueapplication.properties中的s 的依赖关系.(哇!我找不到一个例子,所以也许这会帮助别人.)

片段(下面)设置上下文源,将其注入LdapTemplate到我的DirectoryService中,然后自动装入到我的DirectoryService中.

是否有更好/更清晰的方法来设置ContextSourceSpring Boot应用程序?

application.properties(在类路径上):

ldap.url=ldap://server.domain.com:389
ldap.base:OU=Employees,OU=Users,DC=domain,DC=com
ldap.username:CN=myuserid,OU=employees,OU=Users,DC=domain,DC=com
ldap.password:secretthingy
Run Code Online (Sandbox Code Playgroud)

MyLdapContextSource.java:

@Component
public class MyLdapContextSource extends LdapContextSource implements ContextSource {

    @Value("${ldap.url}")
    @Override
    public void setUrl(String url) { super.setUrl(url);  }

    @Value("${ldap.base}")
    @Override
    public void setBase(String base) {super.setBase(base); }

    @Value("${ldap.username}")
    @Override
    public void setUserDn(String userDn) {super.setUserDn(userDn); }

    @Value("${ldap.password}")
    @Override
    public void setPassword(String password) { super.setPassword(password); }
}
Run Code Online (Sandbox Code Playgroud)

MyLdapTemplate.java:

@Component
public class MyLdapTemplate extends LdapTemplate {

    @Autowired
    public MyLdapTemplate(ContextSource …
Run Code Online (Sandbox Code Playgroud)

java annotations spring-ldap spring-boot

29
推荐指数
2
解决办法
3万
查看次数