Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:52:51+00:00 2026-05-27T20:52:51+00:00

The short question: how can I (or is it possible to) make use of

  • 0

The short question: how can I (or is it possible to) make use of Spring’s transaction manager in classes which are not declared as beans in Spring?

The details…
I have an application using Spring 3 and Hibernate 3.6, and two classes like this:

public class FooManagerImpl implements FooManager {

    @Override
    @Transactional
    public Foo getFoo(int id) {
        Session s = sessionFactory.getCurrentSession();
        // do some query
        return foo;
    }
}

FooManagerImpl is a Spring bean (see below), but FooImpl is not. FooImpl has a parent/child relationship with a third class, Bar, which is also an Entity (this is where things break).

@Entity
@Table(name = "foo_table")
public class FooImpl implements Foo {

    private List<Bar> bars;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    @JoinColumn(name = "foo_id", referencedColumnName = "foo_id")
    public List<Bar> getBar() {
        return this.bars; // Bar is also an entity
    }

    public void addBar(Bar b) {
       this.bar.add(b);
   }
}

And the relevant portions from the Spring configuration:

<tx:annotation-driven />
<jee:jndi-lookup id="fooDatasource" jndi-name="fooDatasource" />
<bean id="fooSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="fooDatasource" />
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <value>com.foo</value>
    </property>
</bean>
<bean id="fooTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="fooSessionFactory" />
    <qualifier value="ec" />
</bean>
<bean id="fooManager" class="com.foo.model.FooManagerImpl">
    <property name="sessionFactory" ref="fooSessionFactory" />
</bean>

Now as I understand it, when calling FooImpl.addBar(Bar), Hibernate will try to persist the changes, which fails because there’s no transaction bound. There’s no TX bound because Spring isn’t proxying for any of the methods in FooImpl because it’s not a bean. Is that a correct assessment? Is there a way I can make this work?

Solutions I’ve found so far, but that don’t seem to quite work (or I can’t get them to work):

  • Setup an AOP proxy to insert transactional advice, like here. But as far as I can tell, those proxies will only apply if I’ve defined the class as a bean, right?

  • I think I can also use AspectJ, such as <tx:annotation-driven mode="aspectj"/>, but that seems to require I use a different classloader, which for various reasons, I can’t do (requires a change to Tomcat, rather than just my webapp… I think). See here.

  • I don’t really want to use OpenSessionInView because I don’t really like the idea of backend transactions occurring outside of the service layer.

So, I’m stumped. Does anyone have any ideas? Should one of the things I’ve tried already have worked? (Possible I screwed it up.)

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:52:52+00:00Added an answer on May 27, 2026 at 8:52 pm

    Not need to declare the entity (FooImpl) as a spring bean. you need this

    <bean id="fooSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="fooDatasource" />
        <property name="annotatedClasses">
            <list>
               <value>com.foo.model.FooImpl</value>     // Entity
            </list>
        </property>
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <value>com.foo</value>
    </property>
    

    view this example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short question, but can't make it work. I have a string: COMPANY NAME -
Short question: Can anyone tell me what the requirements (especially when it comes to
A short question: I've got a TabNavigator with multiple canvas children. How can i
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
The short version of the question - why can't I do this? I'm restricted
This question will be short and sweet. I know an instruction can occur between
I am not sure if I can post this sort of question (apologies in
Short question: Is it possible to do a redirection, say when a user isn't
I'm not sure how to title the question I have so I can't be
I have a short question: How is it possible to convert a String, containing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.