Контекст:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip-2.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="Main.*" />
<aop:aspectj-autoproxy />
<task:annotation-driven/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_example" />
<property name="username" value="root" />
<property name="password" value="tully" />
</bean>
<!-- server beans -->
<bean id="byteArrayCrLfSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer" />
<int-ip:tcp-connection-factory id="tcpServerFactory"
type="server"
port="23234"
single-use="false"
serializer="byteArrayCrLfSerializer"
deserializer="byteArrayCrLfSerializer"
/>
<int:channel id="serverIn" />
<int:channel id="serverOut" />
<int-ip:tcp-inbound-channel-adapter channel="serverIn" connection-factory="tcpServerFactory"/>
<int-ip:tcp-outbound-channel-adapter channel="serverOut" connection-factory="tcpServerFactory"/>
<int:service-activator ref="senderService" method="send" input-channel="serverIn"/>
<!-- Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.baeldung.spring.persistence.model" />
<property name="annotatedClasses">
<list>
<value>Main.database.dataSet.ComplexDataSet</value>
<value>Main.database.dataSet.CameraDataSet</value>
<value>Main.database.dataSet.CommunityDataSet</value>
<value>Main.database.dataSet.JournalDataSet</value>
<value>Main.database.dataSet.RegionDataSet</value>
<value>Main.database.dataSet.SpotDataSet</value>
<value>Main.database.dataSet.TypeDataSet</value>
<value>Main.database.dataSet.VersionDataSet</value>
<value>Main.database.dataSet.CountThreadDataSet</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
SpotDataSet
@Entity
@Table(name = "spot")
public class SpotDataSet implements Serializable {
@Id
@Column(name = "spot_id")
@GeneratedValue
private long spot_id;
@Column(name = "maps")
private String maps;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="region_id")
private RegionDataSet region;
@OneToMany(fetch = FetchType.LAZY,mappedBy = "spot")
private Set<ComplexDataSet> complexDataSets;
......
}
ComplexDataSet
@Entity
@Table(name = "complex")
public class ComplexDataSet implements Complex,Serializable {
@Id
@Column(name = "id")
@GeneratedValue
private long id;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name="spot_id")
private SpotDataSet spot;
@Column(name = "priority")
private int priority;
@Column(name = "type")
private int type;
@Column(name = "ip")
private String ip;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "version_id")
private VersionDataSet version;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name="community_id")
private CommunityDataSet community;
@OneToMany(fetch = FetchType.LAZY,mappedBy = "complex")
private Set<CameraDataSet> cameraDataSets;
.....
}
DBService
@Transactional
public List<SpotDataSet> getAllSpotDataSet(){
Session session = sessionFactory.getCurrentSession();
MySqlDAO dao = new MySqlDAO(session);
return dao.getAllSpotDataSet();
}
DAO
public List<SpotDataSet> getAllSpotDataSet(){
Criteria criteria=session.createCriteria(SpotDataSet.class);
return (List<SpotDataSet>) criteria.list();
}
Main
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");
DBService dbService=ctx.getBean(DBService.class);
List<SpotDataSet> spotDataSets=dbService.getAllSpotDataSet();
System.out.println(spotDataSets.get(0).getComplexDataSets().size());
Error
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Main.database.dataSet.SpotDataSet.complexDataSets, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:155)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:160)
Projections.distinct– MrFylypenko Dec 21 '16 at 10:44distinctпоставил, сейчас у себя проверил все работает, ответ обновил – MrFylypenko Dec 21 '16 at 11:42