{"id":9514,"date":"2024-10-05T11:42:05","date_gmt":"2024-10-05T02:42:05","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9514"},"modified":"2025-06-09T17:50:26","modified_gmt":"2025-06-09T08:50:26","slug":"could-not-initialize-proxy-no-session","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9514","title":{"rendered":"could not initialize proxy &#8211; no Session"},"content":{"rendered":"<h1>could not initialize proxy &#8211; no Session<\/h1>\n<p>\uac1c\ub150\uc744 \uc124\uba85\ud558\uae30 \uc704\ud574 \uc791\uc131\ud55c \ucf54\ub4dc\uc785\ub2c8\ub2e4.<br \/>\n\ud14c\uc2a4\ud2b8\ub294 \uac70\uce58\uc9c0 \uc54a\ub294 \ucf54\ub4dc\uc774\ub2c8 \uc624\ub958\uac00 \uc788\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h2>\uc18c\uc2a4\ucf54\ub4dc<\/h2>\n<pre><code class=\"language-java\">@Entity\n@Table(name = &quot;user&quot;)\npublic class User {\n\n    @Id\n    @GeneratedValue(strategy = GenerationType.IDENTITY)\n    @Column(name = &quot;id&quot;)\n    private int id;\n\n    @Column(name = &quot;first_name&quot;)\n    private String firstName;\n\n    @Column(name = &quot;last_name&quot;)\n    private String lastName;\n\n    @OneToMany\n    private Set&lt;Role&gt; roles;\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@Entity\n@Table(name = &quot;role&quot;)\npublic class Role {\n\n    @Id\n    @GeneratedValue(strategy = GenerationType.IDENTITY)\n    @Column(name = &quot;id&quot;)\n    private int id;\n\n    @Column(name = &quot;role_name&quot;)\n    private String roleName;\n\n}<\/code><\/pre>\n<h2>\uc624\ub958 \ucf54\ub4dc<\/h2>\n<pre><code class=\"language-java\">public UserDto getUser(Long userId) {\n    User user = findUserById(userId);\n\n  return UserDto.builder()\n          .id(user.getId())\n          .firstName(user.getFirstName())\n          .lastName(user.getLastName())\n          .roles(user.getRoles())\n          .build();\n}<\/code><\/pre>\n<h2>\uc6d0\uc778<\/h2>\n<ol>\n<li>JPA\uc5d0\uc11c Session \uc740 \uc601\uc18d\uc131 \ucee8\ud14d\uc2a4\ud2b8\ub97c \uc758\ubbf8\ud55c\ub2e4.<\/li>\n<li>\uc601\uc18d\uc131 \ucee8\ud14d\uc2a4\ud2b8 \uc548\uc5d0\uc11c JPA \uac1d\uccb4\ub4e4\uc774 \uad00\ub9ac\ub41c\ub2e4.<\/li>\n<li>\uc601\uc18d\uc131 \ucee8\ud14d\uc2a4\ud2b8\ub294 \ud2b8\ub80c\uc81d\uc158 \uc2dc\uc791\uc2dc \uc0dd\uc131\ub418\uace0 \ud2b8\ub80c\uc81d\uc158 \uc885\ub8cc\uc2dc \uc0ac\ub77c\uc9c4\ub2e4.<\/li>\n<\/ol>\n<pre><code class=\"language-java\">User user = findUserById(userId);<\/code><\/pre>\n<p>JPA \ub294 \uae30\ubcf8\uc801\uc73c\ub85c \ud544\uc694\ud55c \ucd5c\uc18c\ud55c\uc758 DB \uc811\uadfc\uc744 \ud558\ub3c4\ub85d \ub418\uc5b4 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<p><code>@Transactional<\/code> \uc124\uc815\uc774 \uc5c6\ub294 \uacbd\uc6b0,<br \/>\n\ud2b8\ub80c\uc81d\uc158\uc740 \uba54\uc18c\ub4dc \ud638\ucd9c \ub2e8\uc704\ub85c \uc774\ub8e8\uc5b4\uc9c0\uace0,<br \/>\n\uc704 \uba54\uc18c\ub4dc \ud638\ucd9c\uc774 \ub05d\ub0a8\uacfc \ub3d9\uc2dc\uc5d0 \ud2b8\ub80c\uc81d\uc158\uc774 \uc885\ub8cc\ub429\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-java\">@OneToMany\nprivate Set&lt;Role&gt; roles;<\/code><\/pre>\n<p>\ubb38\uc81c\ub294 \uc704 \ubd80\ubd84\uc758 DB \ub370\uc774\ud0c0\ub97c \uac00\uc838\uc624\uc9c0 \uc54a\uc740 \uc0c1\ud0dc\uc5d0\uc11c,<br \/>\n\ud2b8\ub80c\uc81d\uc158\uc774 \uc885\ub8cc(no Session) \ub41c \uac83\uc785\ub2c8\ub2e4.<\/p>\n<h2>\ud574\uacb0\ucc45<\/h2>\n<h3>@Transactional \ucd94\uac00<\/h3>\n<p>\ud574\uacb0\ucc45 \uc911 \ud558\ub098\ub294 \uc624\ub958\uac00 \ubc1c\uc0dd\ud55c \uba54\uc18c\ub4dc\ub97c \ud2b8\ub80c\uc81d\uc158\uc73c\ub85c \ubb36\uc5b4\uc8fc\ub294 \uac83\uc785\ub2c8\ub2e4.<br \/>\n\ubb38\uc81c\ub294 \ub450\ubc88\uc758 \ucffc\ub9ac\ub85c \ubd84\ub9ac\ub418\uc5b4 \ub370\uc774\ud0c0\ubca0\uc774\uc2a4\uc5d0 \uc811\uadfc\ud558\uac8c \ub429\ub2c8\ub2e4.<br \/>\n(user \ud14c\uc774\ube14 \uc870\ud68c \ud55c\ubc88, role \ud14c\uc774\ube14 \uc870\ud68c \ud55c\ubc88)<\/p>\n<pre><code class=\"language-java\">@Transactional\npublic UserDto getUser(Long userId) { ... }<\/code><\/pre>\n<h3>FetchType.Eager<\/h3>\n<p>\ub610 \ud558\ub098\ub294 <code>FetchType.Eager<\/code> \ub97c \uc124\uc815\ud558\ub294 \uac83\uc785\ub2c8\ub2e4.<br \/>\n\uc774\ub807\uac8c \ub418\uba74 \ud56d\uc0c1 roles \uc815\ubcf4\ub3c4 \ub3d9\uc2dc\uc5d0 \uac00\uc838\uc624\uac8c \ub429\ub2c8\ub2e4.<br \/>\n\ud574\ub2f9 \uc815\ubcf4\uac00 \ubd88\ud544\uc694\ud55c \uc0c1\ud669\ub3c4 \uc788\uc744 \uc218 \uc788\ub294\ub370\ub3c4 \ud56d\uc0c1 \uc870\ud68c\ub97c \ud558\ub294 \ubc29\ubc95\uc774\ubbc0\ub85c \uc88b\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<\/p>\n<p>\uc774 \ubc29\uc2dd\uc758 \uac00\uc7a5 \ud070 \ubb38\uc81c\ub294 User \ubaa9\ub85d \uac80\uc0c9 \uae30\ub2a5\uc774 \ud544\uc694\ud560 \uacbd\uc6b0,<br \/>\n\uc5c4\uccad\ub09c \uc131\ub2a5\uc800\ud558\uac00 \ubc1c\uc0dd\ud569\ub2c8\ub2e4.<\/p>\n<p><code>@Transactional<\/code> \ubc29\uc2dd\uc740 user \ud14c\uc774\ube14 \ud55c\ubc88, role \ud14c\uc774\ube14 \ud55c\ubc88\uc758 \uc870\ud68c\uac00 \uc2e4\ud589\ub418\ub294\ub370,<br \/>\n<code>FetchType.Eager<\/code> \ub294 user \ud14c\uc774\ube14\uc744 \ud55c\ubc88 \uc870\ud68c\ud574\uc11c 1000\uac1c\uc758 \ub370\uc774\ud0c0\ub97c \uac00\uc838\uc654\ub2e4\uba74,<br \/>\nrole \ud14c\uc774\ube14\uc744 1000\ubc88 \ucd94\uac00\ub85c \uc870\ud68c\ud574\uc11c \ub370\uc774\ud0c0\ub97c \uac00\uc838\uc624\uac8c \ub429\ub2c8\ub2e4.<br \/>\n(N+1 \ubb38\uc81c)<\/p>\n<pre><code class=\"language-java\">@OneToMany(fetch = FetchType.Eager)\nprivate Set&lt;Role&gt; roles;<\/code><\/pre>\n<h3>Fetch Join<\/h3>\n<p>\ub610 \ud558\ub098\ub294 <code>Fetch Join<\/code> \uc744 \ud558\ub294 \uac83\uc785\ub2c8\ub2e4.<br \/>\n<code>FetchType.Eager<\/code> \uc640 \uae30\ub2a5\uc740 \ub3d9\uc77c\ud558\uc9c0\ub9cc,<br \/>\n\ucffc\ub9ac \ub2e8\uc704\ub85c Fetch \ubaa8\ub4dc\ub97c \uc870\uc808\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-java\">SELECT u FROM User u JOIN FETCH u.roles<\/code><\/pre>\n<p>\uad6c\ud604 \ubc29\ubc95\uc740 JPQL \uc774 \ub420\uc218\ub3c4 \uc788\uace0, QueryDSL \uc774 \ub420\uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<p>\ud544\uc694\ud55c \uc2dc\uc810\uc5d0 \ud544\uc694\ud55c \ub370\uc774\ud0c0\ub97c \ud55c\ubc88\uc5d0 \uac00\uc838\uc624\ub294 \uc720\uc77c\ud55c \ubc29\ubc95\uc785\ub2c8\ub2e4.<\/p>\n<p><font color=red size=2>\uc544\ub2c8\uba74 \uc5d4\ud130\ud2f0\uc5d0 \uc5d4\ud130\ud2f0\uac04 \uad00\uacc4\uc124\uc815\uc744 \uc544\uc608 \uc548\ud558\uace0 \uc11c\ube44\uc2a4 \ub808\uc774\uc5b4\uc5d0\uc11c \ucc98\ub9ac\ud558\ub294 \ubc29\ubc95\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \uac00\uc7a5 \uac04\ub2e8\ud55c \ucc98\ub9ac\ubc95&#8230;<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>could not initialize proxy &#8211; no Session \uac1c\ub150\uc744 \uc124\uba85\ud558\uae30 \uc704\ud574 \uc791\uc131\ud55c \ucf54\ub4dc\uc785\ub2c8\ub2e4. \ud14c\uc2a4\ud2b8\ub294 \uac70\uce58\uc9c0 \uc54a\ub294 \ucf54\ub4dc\uc774\ub2c8 \uc624\ub958\uac00 \uc788\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc18c\uc2a4\ucf54\ub4dc @Entity @Table(name = &quot;user&quot;) public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = &quot;id&quot;) private int id; @Column(name = &quot;first_name&quot;) private String firstName; @Column(name = &quot;last_name&quot;) private String lastName; @OneToMany private Set&lt;Role&gt; roles;\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9514\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-9514","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9514"}],"version-history":[{"count":9,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9514\/revisions"}],"predecessor-version":[{"id":10418,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9514\/revisions\/10418"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}