У меня есть класс User
:
@Entity
@Table(name = "USER")
public class User implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
private Long id;
@Size(min = 3, max = 50)
@Column(name = "LOGIN", nullable = false)
private String login;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner", cascade = CascadeType.ALL)
private List<Expense> myExpenses = new ArrayList<Expense>(0);
public User()
{
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getLogin()
{
return login;
}
public void setLogin(String login)
{
this.login = login;
}
public List<Expense> getMyExpenses()
{
return myExpenses;
}
public void setMyExpenses(List<Expense> myExpenses)
{
this.myExpenses = myExpenses;
}
}
И класс Expense
:
@Entity
@Table(name = "EXPENSE")
public class Expense implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "EXPENSE_ID")
private Long id;
@ManyToOne(optional = false)
@JoinColumn(name = "USER_ID")
private User owner;
public Expense()
{
}
public User getOwner()
{
return owner;
}
public void setOwner(User owner)
{
this.owner = owner;
}
}
Я тестирую через junit
тест. Если найти класс User
и сделать метод getMyExpenses()
, то выдаёт пустоту, а при просмотре в отладчике значения переменных пишет "PersistentSet". Как в тестах включить, чтобы подтягивалось маппирование?
HibernateTestConfig
:
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages =
{ "ru.make.alex.web.configuration" }, excludeFilters =
{ @Filter(type = FilterType.ANNOTATION, value = Configuration.class) })
@PropertySource(value =
{ "classpath:application.properties" })
public class HibernateTestConfiguration
{
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory()
{
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[]
{ "ru.make.alex.web.model", "ru.make.alex.web.model.revenue" });
sessionFactory.setHibernateProperties(hibernateProperties());
// sessionFactory.setAnnotatedClasses(User.class,
// UserRole.class,Currency.class,Expense.class,OperationOnExpense.class,PackageOperation.class,
// ProductCategory.class,RoutingOfTransaction.class);
return sessionFactory;
}
@Bean
public DataSource dataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource
.setUrl("jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8");
dataSource.setUsername("lcomsys");
dataSource.setPassword("lcomsys");
return dataSource;
}
private Properties hibernateProperties()
{
Properties properties = new Properties();
properties.put("hibernate.dialect",
"org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.hbm2ddl.auto", "create-drop");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "false");
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s)
{
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}