반응형

org.springframework.data.mapping.PropertyReferenceException: No property name found for type

jpa에서 No property name found for type 엔터티명 뜨면서

인젝션 오류 발생한다.

기존에 만들었던 쿼리 메서드가 생성이 안돼서 발생했다.

기존에 있던 속성 이름으로 쿼리 메서드를 생성 후 엔터티 속성을 변경해도 쿼리 메서드엔 변화가 없어

없는 속성으로 쿼리 메서드를 만들어서 빈 생성이 안되고 인젝션이 안됬던 거다.

반응형
반응형

org.hibernate.LazyInitializationException: could not initialize proxy [com.jejunet.cms.server.domain.Server#216] - no Session

at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169)

at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309)

at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)

at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)

at com.jejunet.cms.server.domain.Server$HibernateProxy$1dTl2oLJ.getName(Unknown Source)

at com.jejunet.cms.server.ServerServiceTest.getServer(ServerServiceTest.java:122)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.base/java.lang.reflect.Method.invoke(Method.java:566)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)

at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)

at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)

at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)


아래와 같이 테스트 메소드를 단순히 저장하고 조회 하는데 예외 발생했다.

단건 가져오는것에 japRepository 기능 구현이 다른가보다.

repository.findByid(id);  /* Retrieves an entity by its id  */

repository.getOne(id);  /* Returns a reference to the entity with the given identifier. */ <= 이놈 쓰면 오류남 

@Test

public void getServer() {

Server savedServer = repository.save(new Server(SERVER_NAME));

Optional<Server> server2 = serverService.getServer(savedServer.getId());

assertEquals(savedServer.getName(), server2.get().getName());

}


아래 블로그에서 싶게 설명하고 있다. 

영속성 관련해서 레퍼런스만 필요할때 경우에 따라 쓴다고 하네요.(복잡함 ㅠㅠ)

https://bebong.tistory.com/entry/JPA-Lazy-Evaluation-LazyInitializationException-could-not-initialize-proxy-%E2%80%93-no-Session 

반응형
반응형


 

 

book, publication relationbook, publication relation



 

 

 

 

@Entity(name = "Book")

@Table(name = "book")

public static class Book

implements Serializable {

@Id

@GeneratedValue

private Long id;

private String title;

private String author;

@NaturalId

private String isbn;

//Getters and setters omitted for brevity

}

 

 

Book은 관계에서의 부모쪽을 나타낸다.

isbn컬럼은 isbn컬럼은 비즈니스 키로 잘 사용할수 있기 때문에 @NaturalId로 맵핑 됩니다.

 

 

 

@Entity(name = "Publication")

@Table(name = "publication")

public static class Publication {

@Id

@GeneratedValue

private Long id;

private String publisher;

@ManyToOne(fetch = FetchType.LAZY)

@JoinColumn(

name = "isbn",

referencedColumnName = "isbn"

)

private Book book;

@Column(

name = "price_in_cents",

nullable = false

)

private Integer priceCents;

private String currency;

//Getters and setters omitted for brevity

}

 

Publication은 관계에서 자식이다.

기본적으로 @ManyToOne 연결에서는 부모측 엔터티 식별자가 자식의 외부키 열과 조인하는데 사용된다고 가정 되나, 비식별자 연관을 할때는 referencedColumnName을 사용하여 ManyToOne 관계를 설정하기 위해 부모측에 어떤컬럼이 사용되어야 하는지를 설정합니다.

 

 

 

Publication publication = entityManager.createQuery(

"select p " +

"from Publication p " +

"join fetch p.book b " +

"where " +

" b.isbn = :isbn and " +

" p.currency = :currency", Publication.class)

.setParameter( "isbn", "978-9730228236" )

.setParameter( "currency", "&" )

.getSingleResult();

assertEquals(

"amazon.co.uk",

publication.getPublisher()

);

assertEquals(

"High-Performance Java Persistence",

publication.getBook().getTitle()

);

 

 

JQPL쿼리를 실행

 

SELECT

p.id AS id1_1_0_, b.id AS id1_0_1_,

p.isbn AS isbn5_1_0_, p.currency AS currency2_1_0_,

p.price_in_cents AS price_in3_1_0_,

p.publisher AS publishe4_1_0_,

b.author AS author2_0_1_, b.isbn AS isbn3_0_1_,

b.title AS title4_0_1_

FROM publication p

INNER JOIN

book b ON p.isbn = b.isbn

WHERE b.isbn = '978-9730228236'

AND p.currency = '&'

반응형
반응형

스프링 APA 하면서 Entity를 화면 전용 DTO 객체를 만들고 객체 변형하면서 삽질 하다가 발견한 유틸이다.


객체에 같은 프로퍼티의 값을 복사해준다.


BeanUtils.copyProperties(boardConfig, this);


void org.springframework.beans.BeanUtils.copyProperties(Object source, Object target) throws BeansException


Copy the property values of the given source bean into the target bean.

Note: The source and target classes do not have to match or even be derived from each other, as long as the properties match. Any bean properties that the source bean exposes but the target bean does not will silently be ignored.

This is just a convenience method. For more complex transfer needs, consider using a full BeanWrapper.

Parameters:
source the source bean
target the target bean
Throws:
BeansException - if the copying failed
See Also:
BeanWrapper


반응형
반응형




@ManyToOne

@JoinColumn(name = "code", referencedColumnName="code", nullable=false)

private Code code;



@JoinColumn 의 내부 속성으로, 참조키 컬럼을 입력한다.

자동키를 적용하고, 비식별관계로 설정했을시 참조키 컬럼을을 설정해야 한다.


@referencedColumnName

반응형
반응형


반응형
반응형

심각: Servlet.service() for servlet [appServlet] in context with path [] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause

java.lang.IllegalStateException: getOutputStream() has already been called for this response


spring + jpa 환경에서


@ResponseBody 하면서 json 으로 응답하면서 jpa 관계 설정이 안맞아서 인지 예외가 발생했다.

Entity 에 @JsonBackReference 를 적당히 사용하면 된다고 나왔다.


@OneToMany 관계에서 일대다 관계에서 @JsonBackReference 써주면 OneToMany관계의 다의 관계 데이터가 Json 으로 잘 변환되어

넘어온다.


흐흐.. 어렵다.

반응형

+ Recent posts