2
回答
@OneToOne延迟加载报错:could not initialize proxy - no Session
科大讯飞通用文字识别100000次/天免费使用。立即申请   

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

请问,怎么解决






举报
Spring-JPA
发帖于2年前 2回/312阅
顶部