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环境,用不了
请问,怎么解决
给你推荐一本书。hibernate实战第二版 今年的新书基于hibernate5的 http://product.dangdang.com/24040630.html
解决了吗
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。