1

我只是 Spring 和 JPA 2 的新手。我知道这个问题很简单,但我找不到任何答案。

通常这就是我在支持 bean 中调用 roo 服务的方式

public Groups getGroups(Long id){
    Groups groups = groupsService.findGroupsById(id);
}

例如; 我有一个 JPA 实体文件名“用户”:

public class UserCredential {

    @Id
    @Column(name = "id")
    private Long id;

    @NotNull
    @Column(unique = true, name = "userName")
    @Size(max = 12)
    private String userName;

    @NotNull
    @Size(max = 80)
    private String password;


    private Long groupId;

    @Transient
    private String getGroupName(){
        String groupName = null;

        // call groupsService here....
        Group groups = groupsService.findGroupsById(this.groupId);

        if(groups != null){
            groupName = groups.getName();
        }
    return groupName;
    }
}

我想groupsService.findGroupsById(id)在 JPA 实体中使用这个“”,以便列表能够显示组名。

我知道 JPA 实体能够将 groupId 链接为 FK,如下所示:

@ManyToOne(targetEntity=Groups.class)
private Groups groupId;

但是如果 groupId 不是 FK 呢?我如何在 JPA 实体中调用服务?上面的方法我试过了,不行。

有什么想法吗?

谢谢你。

4

1 回答 1

1

您可以创建一个具有对 ApplicationContext 的静态引用的 util 类,这是我在项目中使用的代码示例(不记得我从哪里复制了这个类):

package yourpage;
/**
 * Wrapper to always return a reference to the Spring Application Context from
 * within non-Spring enabled beans. Unlike Spring MVC's
 * WebApplicationContextUtils we do not need a reference to the Servlet context
 * for this. All we need is for this bean to be initialized during application
 * startup.
 */
public class SpringApplicationContext implements ApplicationContextAware {

    private static ApplicationContext CONTEXT;

    /**
     * This method is called from within the ApplicationContext once it is done
     * starting up, it will stick a reference to itself into this bean.
     * 
     * @param context
     *            a reference to the ApplicationContext.
     */
    public void setApplicationContext(ApplicationContext context)
            throws BeansException {
        CONTEXT = context;
    }

    public static void setApp(ApplicationContext context){
        CONTEXT = context;
    }

    /**
     * This is about the same as context.getBean("beanName"), except it has its
     * own static handle to the Spring context, so calling this method
     * statically will give access to the beans by name in the Spring
     * application context. As in the context.getBean("beanName") call, the
     * caller must cast to the appropriate target class. If the bean does not
     * exist, then a Runtime error will be thrown.
     * 
     * @param beanName
     *            the name of the bean to get.
     * @return an Object reference to the named bean.
     */
    public static Object getBean(String beanName) {
        return CONTEXT.getBean(beanName);
    }
}

使用以下标记将此类添加到 springContext.xml 中:

<bean id="springApplicationContext" class="yourpackage.SpringApplicationContext"/>

用法

//get the service bean of the spring context    
    GroupService groupService = (GroupService) SpringApplicationContext.getBean("groupService");
    groupService.findGroupsById(id);
于 2013-06-11T16:24:37.920 回答