@OneToOne延迟加载报错:could not initialize proxy
spring data jpa版本:1.10.4.RELEASE
hibernate版本:5.1.2.Final
web.xml代码如下:
<dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.10.4.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.2.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.1.2.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="users") public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column private String name; @OneToOne(cascade={CascadeType.PERSIST,CascadeType.REMOVE}, fetch=FetchType.LAZY) @JoinColumn(name="card_id") private Card card; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } public String toString() { return "User [id=" + id + ", name=" + name + ", card=" + card + "]"; } }
@Entity @Table(name="cards") public class Card { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column private String num; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNum() { return num; } public String toString() { return "Card [id=" + id + ", num=" + num + "]"; } public void setNum(String num) { this.num = num; } }
import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository<User, Integer> { }
@ComponentScan @EnableJpaRepositories @EnableTransactionManagement public class App { public static void main( String[] args ) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(App.class); UserRepository br = context.getBean(UserRepository.class); System.out.println(br.findOne(1)); context.close(); } }
Hibernate: select user0_.id as id1_1_0_, user0_.card_id as card_id3_1_0_, user0_.name as name2_1_0_ from users user0_ where user0_.id=? Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:147) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:260) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73) at com.pp.spring.data.jpa.Card_$$_jvst17b_0.toString(Card_$$_jvst17b_0.java) at java.lang.String.valueOf(String.java:2994) at java.lang.StringBuilder.append(StringBuilder.java:131) at com.pp.spring.data.jpa.User.toString(User.java:53) at java.lang.String.valueOf(String.java:2994) at java.io.PrintStream.println(PrintStream.java:821) at com.pp.spring.data.jpa.App.main(App.java:15)
网上说,使用OpenSessionInViewFilter解决,但是,我就是一个java se的demo,没有web环境,用不了
请问,怎么解决
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了吗