开发者社区> 问答> 正文

@OneToOne延迟加载报错:could not initialize pro?报错

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环境,用不了

请问,怎么解决






展开
收起
爱吃鱼的程序员 2020-06-08 21:01:29 433 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    给你推荐一本书。hibernate实战第二版 今年的新书基于hibernate5的 http://product.dangdang.com/24040630.html

    解决了吗

    2020-06-08 21:01:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载