Last active
August 29, 2015 14:05
-
-
Save aVolpe/33c9c407a729b101bbaa to your computer and use it in GitHub Desktop.
StackOverflow question #25504340
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.HashSet; | |
import java.util.Properties; | |
import java.util.Set; | |
import javax.persistence.Entity; | |
import javax.persistence.FetchType; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.OneToMany; | |
import javax.persistence.OneToOne; | |
import javax.persistence.SequenceGenerator; | |
import javax.sql.DataSource; | |
import org.hibernate.Session; | |
import org.hibernate.SessionFactory; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | |
import org.springframework.orm.hibernate4.HibernateTransactionManager; | |
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.TestExecutionListeners; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.context.support.AnnotationConfigContextLoader; | |
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; | |
import org.springframework.test.context.support.DirtiesContextTestExecutionListener; | |
import org.springframework.test.context.transaction.TransactionConfiguration; | |
import org.springframework.test.context.transaction.TransactionalTestExecutionListener; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
import org.springframework.transaction.annotation.Transactional; | |
import com.google.common.collect.Sets; | |
@ContextConfiguration(loader = AnnotationConfigContextLoader.class) | |
@Transactional | |
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) | |
@TestExecutionListeners({ TransactionalTestExecutionListener.class, | |
DependencyInjectionTestExecutionListener.class, | |
DirtiesContextTestExecutionListener.class }) | |
@RunWith(SpringJUnit4ClassRunner.class) | |
public class Test25504340 { | |
@Configuration | |
@EnableTransactionManagement() | |
static class ContextConfiguration { | |
@Bean | |
public DataSource dataSource() throws IOException { | |
EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder() | |
.setType(EmbeddedDatabaseType.H2); | |
return edb.build(); | |
} | |
@Bean | |
public LocalSessionFactoryBean sessionFactory() throws IOException { | |
LocalSessionFactoryBean bean = new LocalSessionFactoryBean(); | |
Class<?>[] annonClasses = new Class<?>[] { DefaultManager.class, | |
DefaultAccount.class }; | |
bean.setAnnotatedClasses(annonClasses); | |
bean.setDataSource(this.dataSource()); | |
Properties props = new Properties(); | |
props.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); | |
props.put("hibernate.hbm2ddl.auto", "create-drop"); | |
props.put("hibernate.show_sql", "true"); | |
bean.setHibernateProperties(props); | |
return bean; | |
} | |
@Bean | |
HibernateTransactionManager transactionManager() throws IOException { | |
return new HibernateTransactionManager(this.sessionFactory() | |
.getObject()); | |
} | |
} | |
@Autowired | |
private SessionFactory sessionFactory; | |
@Test | |
public void testName() throws Exception { | |
Account a1 = new DefaultAccount("one", "123"); | |
Account a2 = new DefaultAccount("two", "123"); | |
Account a3 = new DefaultAccount("three", "123"); | |
Manager man = new DefaultManager(a1, Sets.newHashSet(a2, a3)); | |
a1.setManager(man); | |
Session ses = sessionFactory.getCurrentSession(); | |
ses.persist(a1); | |
ses.persist(a2); | |
ses.persist(a3); | |
ses.persist(man); | |
ses.flush(); | |
} | |
public interface Account { | |
public void setManager(Manager man); | |
} | |
@Entity | |
public static class DefaultAccount implements Account { | |
@OneToOne(targetEntity = DefaultManager.class) | |
private Manager manager; | |
@Id | |
String email; | |
String password; | |
protected DefaultAccount() { | |
}; | |
public DefaultAccount(String email, String password) { | |
this.email = email; | |
this.password = password; | |
} | |
public DefaultAccount(String email, String password, Manager manager) { | |
this(email, password); | |
this.manager = manager; | |
} | |
@Override | |
public String toString() { | |
return ToStringBuilder.reflectionToString(this).toString(); | |
} | |
@Override | |
public void setManager(Manager man) { | |
this.manager = man; | |
} | |
} | |
public static interface Manager { | |
public Long getId(); | |
} | |
@Entity | |
@SequenceGenerator(name = "MAN_SEQ", sequenceName = "man_seq") | |
public static class DefaultManager implements Manager { | |
@Id | |
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MAN_SEQ") | |
public Long id; | |
@OneToOne(targetEntity = DefaultAccount.class) | |
private Account managerAccount; | |
@OneToMany(fetch = FetchType.LAZY, mappedBy = "manager", targetEntity = DefaultAccount.class) | |
private Set<Account> accountsToManage = new HashSet<Account>(); | |
protected DefaultManager() { | |
} | |
public DefaultManager(Account managerAccount, | |
Set<Account> accountsToManage) { | |
this.managerAccount = managerAccount; | |
this.accountsToManage.addAll(accountsToManage); | |
} | |
@Override | |
public Long getId() { | |
return id; | |
} | |
@Override | |
public String toString() { | |
return ToStringBuilder.reflectionToString(this).toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment