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

发布于 2021-12-05 05:04:10 字数 4786 浏览 997 评论 1

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深巷少女 2021-12-05 05:33:57

解决了吗

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文