Tony_Ynot
Tony_Ynot

Reputation: 165

How to call spring roo service in spring data JPA entity?

I'm just new to Spring and JPA 2. I know this question is simple , but i just cant find any answer.

Normally this is how i call the roo service in backing bean

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

For Example; I have an JPA entity file name "User":

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;
    }
}

and i want to use this " groupsService.findGroupsById(id) " in the JPA entity, so that the listing is able to display the group name.

I know JPA entity are able to link the groupId as FK like below:

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

but how about if the groupId is not FK ? How can i call the service inside the JPA entity ? i had tried for the above way , it's doesnt work.

Any ideas about this ?

Thank you.

Upvotes: 1

Views: 1120

Answers (1)

fmodos
fmodos

Reputation: 4568

You can create one util class that will have a static reference to the ApplicationContext, here is a code example that I use in my project(cant remember where I copied this class from):

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);
    }
}

Add this class to the springContext.xml with the tag below:

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

Usage

//get the service bean of the spring context    
    GroupService groupService = (GroupService) SpringApplicationContext.getBean("groupService");
    groupService.findGroupsById(id);

Upvotes: 1

Related Questions