ref by

https://velog.io/@ohzzi/Entity-DAO-DTO가-무엇이며-왜-사용할까

 

Entity, DAO, DTO가 무엇이며 왜 사용할까?

개인적으로 Spring Boot를 가지고 CRUD를 구현한 Todo-list를 만들어면서, Spring Data JPA를 사용하게 되었다. JPA를 사용하면서, 생전 처음 보는 Entity, DAO, DTO 개념을 사용하게 되었는데, 앞으로 계속 많이

velog.io

 

개인적으로 Spring Boot를 가지고 CRUD를 구현한 Todo-list를 만들어면서, Spring Data JPA를 사용하게 되었다. JPA를 사용하면서, 생전 처음 보는 Entity, DAO, DTO 개념을 사용하게 되었는데, 앞으로 계속 많이 쓰게 될 것 같아서 정리하는 시간을 가지려고 한다.

Entity란?

Entity 클래스는 DB의 테이블에 존재하는 Column들을 필드로 가지는 객체를 말한다. Entity는 DB의 테이블과 1대 1로 대응되며, 때문에 테이블이 가지지 않는 컬럼을 필드로 가져서는 안된다. 또한 Entity 클래스는 다른 클래스를 상속받거나 인터페이스의 구현체여서는 안된다.

예를 들어 post 라는 테이블이 title, content, author 라는 컬럼들을 가지고 있다면, 이에 대응하는 Entity 클래스 Post는

@Entity public class Post { private String title; private String content; private String author; }

와 같이 post 테이블의 컬럼들을 필드로 가져야 한다.

JPA를 사용할 때 Entity 클래스에는 @Entity 어노테이션을 붙여서 Entity임을 명시해 줘야 하며, 내부의 필드에는 @Column, @Id 어노테이션 등을 사용한다. Entity는 외부에서 최대한 Entity의 Getter를 사용하지 않도록 내부에 로직을 구현하는데, 이 때 Domain 로직만 구현하고 Presentation 로직은 구현하지 않는다.

Entity의 Getter 사용을 최대한 피하라고 했지만, 기본적으로 Entity를 만들 때 Getter는 만들어줘야 한다. 그런데 이동욱님이 쓰신 책을 보니, Entity 클래스에서 Setter를 만드는 것은 피하라는 조언이 있었다.

일반적으로 자바 클래스를 만들때 private 필드들을 가지고 습관적으로 Getter와 Setter를 만들고는 하는데, 문제는 Setter의 사용이 Entity의 일관성을 해칠 수 있다는 것이다.

Setter를 무분별하게 사용하게 되면, Entity의 인스턴스 값들이 언제 어디서 변하는지 명확히 알 수 없다. 따라서 Setter 대신 다른 방법으로 필드에 값을 넣어 주는 것이 좋다. 그런데 일반적으로 생각할 수 있는 인스턴스의 생성 시점에 생성자로 필드에 값을 넣어주는 방법 또한 그다지 좋지 않은 방법일 수 있는데, 생성자에 현재 넣는 값이 어떤 필드인지 명확히 알 수 없고, 매개변수끼리의 순서가 바뀌더라도 코드가 모두 실행되기 전까지는 문제를 알 수 없다는 단점이 있기 때문이다.

따라서 Builder 패턴을 사용하는 것이 가장 좋다. 멤버 변수가 많아지더라도 어떤 값을 어떤 필드에 넣는지 코드를 통해 확인할 수 있고, 필요한 값만 집어넣는 것이 가능하기 때문이다.

Entity 클래스 생성 및 Builder 패턴 예시@Getter @Entity @NoArgsConstructor public class Membmer member { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String name; private String email; @Column(length = 13, nullable = false) private String phoneNumber; @Builder public Member(long id, String name, String email, String phoneNumber) { this.id = id; this.name = name; this.email = email; this.phoneNumber = phoneNumber; } } // 사용 방법 Member member = new member.builder() .name("홍길동") .email("test@gmail.com") .phoneNumber("010-1234-5678") .build();

DAO란?

DAO는 Data Access Object의 약자로, 실제로 DB에 접근하는 객체다. DAO는 프로젝트의 서비스 모델과 실제 데이터베이스를 연결하는 역할을 하며, JPA에서는 DB에 데이터를 CRUD 하는 Repository 객체들이 DAO라고 볼 수 있다.

엄밀히 말하면 Repository가 DAO를 대체한다고 보는 것이 맞는 것 같은데, DAO와 Repository 모두 DB에 직접 쿼리를 날려 CRUD를 하는 것은 동일하나, 개념적인 측면에서 차이가 있는 것 같다. 참고 자료를 한번 가져와봤다.

DAO와 REPOSITORY 모두 퍼시스턴스 로직에 대한 객체-지향적인 인터페이스를 제공하고 도메인 로직과 퍼시스턴스 로직을 분리하여 관심의 분리(separation of concerns) 원리를 만족시키는데 목적이 있다. 그러나 비록 의도와 인터페이스의 메소드 시그니처에 유사성이 존재한다고 해서 DAO와 REPOSITORY를 동일한 패턴으로 취급하는 것은 성급한 일반화의 오류를 범하는 것이다.
DAO는 퍼시스턴스 로직인 Entity Bean을 대체하기 위해 만들어진 개념이다. DAO가 비록 객체-지향적인 인터페이스를 제공하려는 의도를 가지고 있다고 하더라도 실제 개발 시에는 하부의 퍼시스턴스 메커니즘이 데이터베이스라는 사실을 숨기려고 하지 않는다. DAO의 인터페이스는 데이터베이스의 CRUD 쿼리와 1:1 매칭되는 세밀한 단위의 오퍼레이션을 제공한다. 반면 REPOSITORY는 메모리에 로드된 객체 컬렉션에 대한 집합 처리를 위한 인터페이스를 제공한다. DAO가 제공하는 오퍼레이션이 REPOSITORY 가 제공하는 오퍼레이션보다 더 세밀하며, 결과적으로 REPOSITORY에서 제공하는 하나의 오퍼레이션이 DAO의 여러 오퍼레이션에 매핑되는 것이 일반적이다. 따라서 하나의 REPOSITORY 내부에서 다수의 DAO를 호출하는 방식으로 REPOSITORY를 구현할 수 있다.
출처 : http://egloos.zum.com/aeternum/v/1160846

솔직히 완벽히 이해되지는 않는다. 이 부분은 개념적으로 좀 더 공부가 필요할 것 같다.

DTO란?

DTO는 Data Transfer Object의 약자로, 계층 간 데이터 교환 역할을 한다. DB에서 꺼낸 데이터를 저장하는 Entity를 가지고 만드는 일종의 Wrapper라고 볼 수 있는데, Entity를 Controller 같은 클라이언트단과 직접 마주하는 계층에 직접 전달하는 대신 DTO를 사용해 데이터를 교환한다.

DTO는 그저 계층간 데이터 교환이 이루어 질 수 있도록 하는 객체이기 때문에, 특별한 로직을 가지지 않는 순수한 데이터 객체여야 한다. 또한 DB에서 꺼낸 값을 DTO에서 임의로 조작할 필요가 없기 때문에 DTO에는 Setter를 만들 필요가 없고 생성자에서 값을 할당한다. 개인적으로는 생성자 또한 사용하지 않고 Entity처럼 Builder 패턴을 통해 값을 할당하는 것이 가장 좋은 것 같다.

Entity와 DTO를 분리하는 이유

Entity의 값이 변하면 Repository 클래스의 Entity Manager의 flush가 호출될 때 DB에 값이 반영되고, 이는 다른 로직들에도 영향을 미친다. 때문에 View와 통신하면서 필연적으로 데이터의 변경이 많은 DTO 클래스를 분리해주어야 한다.

또한 도메인 설계가 아무리 잘 되었다 해도 Getter만을 이용해서 원하는 데이터를 표시하기 어려운 경우가 발생할 수 있는데, 이 경우에 Entity와 DTO가 분리되어 있지 않다면 Entity 안에 Presentation을 위한 필드나 로직이 추가되게 되어 객체 설계를 망가뜨리게 된다. 때문에 이런 경우에는 분리한 DTO에 Presentation 로직 정도를 추가해서 사용하고, Entity에는 추가하지 않아서 도메인 모델링을 깨뜨리지 않는다.

DTO와 VO와의 차이

VO(Value Object)도 DTO와 동일한 개념이다. 다만 DTO와의 차이는, DTO는 데이터를 계층간 교환(Transfer)하는데 의미가 있고, VO는 읽기만 가능한 read-only 속성을 가진 객체로서 데이터 그 자체에 의미를 두고 있다는 점이다.

참고자료
https://gmlwjd9405.github.io/2018/12/25/difference-dao-dto-entity.html
http://egloos.zum.com/aeternum/v/1160846
https://iri-kang.tistory.com/5

 

'program > database' 카테고리의 다른 글

mongodb user 생성관리(몽고 db 공식)  (0) 2021.05.27
mongodb user 생성 관리  (0) 2021.05.27
database dml / ddl / dcl  (0) 2021.05.14

Add Users

Overview

MongoDB employs role-based access control (RBAC) to determine access for users. A user is granted one or more roles that determine the user's access or privileges to MongoDB resources and the actions that user can perform. A user should have only the minimal set of privileges required to ensure a system of least privilege.

Each application and user of a MongoDB system should map to a distinct user. This access isolation facilitates access revocation and ongoing user maintenance.

Prerequisites

If you have enabled access control for your deployment, you can use the localhost exception to create the first user in the system. This first user must have privileges to create other users. As of MongoDB 3.0, with the localhost exception, you can only create users on the admin database. Once you create the first user, you must authenticate as that user to add subsequent users. Enable Access Control provides more detail about adding users when enabling access control for a deployment.

For routine user creation, you must possess the following permissions:

The userAdmin and userAdminAnyDatabase built-in roles provide createUser and grantRole actions on their respective resources.

Examples

To create a user in a MongoDB deployment, you connect to the deployment, and then use the db.createUser() method or createUser command to add the user.

Username/Password Authentication

The following operation creates a user in the reporting database with the specified name, password, and roles.

TIP

Starting in version 4.2 of the mongo shell, you can use the passwordPrompt() method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.

use reporting
db.createUser(
{
user: "reportsUser",
pwd: passwordPrompt(), // or cleartext password
roles: [
{ role: "read", db: "reporting" },
{ role: "read", db: "products" },
{ role: "read", db: "sales" },
{ role: "readWrite", db: "accounts" }
]
}
)

 

Enable Access Control provides more details about enforcing authentication for your MongoDB deployment.

Kerberos Authentication

Users that will authenticate to MongoDB using an external authentication mechanism, such as Kerberos, must be created in the $external database, which allows mongos or mongod to consult an external source for authentication.

Changed in version 3.6.3: To use sessions with $external authentication users (i.e. Kerberos, LDAP, x.509 users), the usernames cannot be greater than 10k bytes.

For Kerberos authentication, you must add the Kerberos principal as the username. You do not need to specify a password.

The following operation adds the Kerberos principal reportingapp@EXAMPLE.NET with read-only access to the records database.

use $external
db.createUser(
{
user: "reportingapp@EXAMPLE.NET",
roles: [
{ role: "read", db: "records" }
]
}
)

 

Configure MongoDB with Kerberos Authentication on Linux and Configure MongoDB with Kerberos Authentication on Windows provide more details about setting up Kerberos authentication for your MongoDB deployment.

LDAP Authentication

Users that will authenticate to MongoDB using an external authentication mechanism, such as LDAP, must be created in the $external database, which allows mongos or mongod to consult an external source for authentication.

Changed in version 3.6.3: To use sessions with $external authentication users (i.e. Kerberos, LDAP, x.509 users), the usernames cannot be greater than 10k bytes.

For LDAP authentication, you must specify a username. You do not need to specify the password, as that is handled by the LDAP service.

The following operation adds the reporting user with read-only access to the records database.

use $external
db.createUser(
{
user: "reporting",
roles: [
{ role: "read", db: "records" }
]
}
)

 

Authenticate Using SASL and LDAP with ActiveDirectory and Authenticate Using SASL and LDAP with OpenLDAP provide more detail about using authenticating using LDAP.

x.509 Client Certificate Authentication

Users that will authenticate to MongoDB using an external authentication mechanism, such as x.509 Client Certificate Authentication, must be created in the $external database, which allows mongos or mongod to consult an external source for authentication.

Changed in version 3.6.3: To use sessions with $external authentication users (i.e. Kerberos, LDAP, x.509 users), the usernames cannot be greater than 10k bytes.

For x.509 Client Certificate authentication, you must add the value of the subject from the client certificate as a MongoDB user. Each unique x.509 client certificate corresponds to a single MongoDB user. You do not need to specify a password.

The following operation adds the client certificate subjectCN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry user with read-only access to the records database.

use $external
db.createUser(
{
user: "CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry",
roles: [
{ role: "read", db: "records" }
]
}
)

 

Use x.509 Certificates to Authenticate Clients provides details about setting up x.509 Client Certificate authentication for your MongoDB deployment.

 

 

 

 

Enable Access Control — MongoDB Manual

Starting in version 4.2 of the mongo shell, you can use the passwordPrompt() method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command cal

docs.mongodb.com

 

 

'program > database' 카테고리의 다른 글

entity dao dto 컨셉  (0) 2021.05.27
mongodb user 생성 관리  (0) 2021.05.27
database dml / ddl / dcl  (0) 2021.05.14

https://emong.tistory.com/237

 

mongodb 사용자 관리

MongoDB 설치후 데이터베이스 위치, 로그, 인증 등에 관련한 서버 구성과 설정을 정리한다. MongoDB 2.6 과 MongoDB Community Edition 3.x 버전을 사용했다. mongoDB 접근제어 mongoDB 는 설치과정 중에 인증과..

emong.tistory.com

 

기본적으로 셋업 후 유저나 admin셋팅이 되어 있지 않다.

 

mongoDB 접근제어

mongoDB 는 설치과정 중에 인증과 관련해 설정하는 부분이 없어서 설치 후 누구나 DB에 접속 할 수 있다. 인증을 추가해 데이터베이스 관리자와 데이터베이스 사용자로 구분해서 이용하고, 각 데이터베이스의 사용자는 허가된 역할(Role)을 가지고 데이터베이스에 접근 가능하도록 구성한다.

여기서는 다음 두 가지를 다루고 있다.

  • (1) 데이터베이스 관리자 추가
  • (2) 데이터베이스 사용자 추가

Ubuntu/Debian 리눅스 배포본에 MongoDB 3.x 버전이 지원되지 않으면, MongoDB Community Edition 를 패키지 혹은 소스로 설치할 수 있다.

데이터베이스 관리자

mongod가 비인증 모드로 실행중인 상태에서, mongo 클라이언트로 데이터베이스에 접속한다.접속에 성공하면 > 프롬프트가 표시된다. 그리고 접속한 후에 admin 데이터베이스로 전환한다.

> use admin switched to db admin >

mongo 클라이언트로 접속해 mongoDB 데이터베이스 관리자 admin 추가해서, 사용자 롤로 userAdminAnyDatabase 롤을 추가해준다.

mongoDB 2.6 이후 관리자 계정 추가

mongoDB 2.6 이후는 db.createUser() 로 사용자를 추가한다. 1 다음은 admin 데이터베이스에서 사용자를 관리하는 admin 계정을 생성하고 있다.

> > db.createUser({ user:'admin', pwd:'****', roles:['userAdminAnyDatabase'] }) Successfully added user: { "user" : "admin", "roles" : [ "userAdminAnyDatabase" ] } > > db.getUsers() // 데이터베이스 사용자 확인 [ { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } ]

mongoDB 2.4 이전 관리자 계정 추가

mongoDB 2.4 까지는 새로운 사용자는 db.addUser() 로 추가한다.2

$ mongo // mongo client 로 접속 >use admin // admin DB 사용 >db.addUser( { user: "<username>", // admin name pwd: "<password>", roles: [ "userAdminAnyDatabase" ] // Database role } )

mongoDB 2.6까지 32bit 버전을 지원하고 있다.

관리자 계정을 만든후 MongoDB에 mongo 클라이언트로 인증 로그인을 한 후에 데이터베이스를 생성하고 해당 데이터베이스 사용자에 접근 권한을 추가해 준다.

데이터 베이스 생성과 롤 기반 인증

관리자 로그인

이제 데이터베이스 관리자 계정으로 로그인해서 사용하려는 데이터베이스를 use로 선택하고 해당 데이터베이스 사용자를 추가해준다.

mongo 클라이언트 로그인시 -u <username>-p <password> 와 --authenticationDatabase <database> 를 지정해 주어야 한다.

$ mongo --port 27017 -u "admin" -p "****" --authenticationDatabase "admin" MongoDB shell version v3.4.0 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.0

혹은 인증없이 mongo 클라이언트로 데이터베이스에 접속한 후에 db.auth() 명령을 사용할 수 있다.

> use admin switched to db admin > db.auth("admin", "****") 1 > show users { "_id" : ObjectId("5733676238ac1ddf4cf745c2"), "user" : "admin", "readOnly" : false, "pwd" : "24413999168dccff96dcc735720c85ce" } >

이제 각 데이터베이스에 사용자를 생성해서 사용해서 인증한 사용자만 데이터베이스를 사용하게 할 수 있다.

Database 사용자 추가

mongoDB v3.x 사용자 관리

mongoDB v2.6 히우는 대부분 mongoDB v3.4와 호환되는 사용자 관리 명령을 사용한다. 여기서는 User Management Methods (v3.4)를 참고하고 있다.

NameDescription

db.auth() 데이터베이스에 사용자 인증
db.createUser() Creates a new user.
db.updateUser() Updates user data.
db.changeUserPassword() 사용자 패스워드 변경
db.dropAllUsers() 데이터베이스에 관련된 모든 사용자를 삭제한다.
db.dropUser() 한 사용자를 삭제한다
db.grantRolesToUser() 롤과 권한을 사용자에 허용한다
db.revokeRolesFromUser() 사용자에 부여한 롤을 삭제한다
db.getUser() 지정한 사용자의 정보를 반환한다
db.getUsers() 데이터베이스에 관련된 모든 사용자의 정보를 반환한다

createUser()

db.createUser() 는 두 개의 도큐멘트를 인자로 사용한다:

db.createUser(user, writeConcern)

여기서 user 도큐멘트는 아래 같은 형식을 갖는다:

{ user: "<name>", pwd: "<cleartext password>", customData: { <any information> }, roles: [ { role: "<role>", db: "<database>" } | "<role>", ... ] }

  • customData: 선택적으로 추가할 정보를 담은 도큐멘트.

다음은 product 데이터베이스로 전환해서 product 데이터베이스 사용자를 추가하고 있다. customeData 를 주목하자.

> use products db.createUser( { user: "user1", pwd: "changeMe", customData: { employeeId: 12345 }, // prducts roles: [ { role: "clusterAdmin", db: "admin" }, { role: "readAnyDatabase", db: "admin" }, "readWrite"] }, { w: "majority" , wtimeout: 5000 } )

다양한 사례는 createUser() Exmaple 를 참고하자.

mongoDB v2.4는 사용자 추가 방법이 조금 다르다.



출처: https://emong.tistory.com/237 [에몽이]

'program > database' 카테고리의 다른 글

entity dao dto 컨셉  (0) 2021.05.27
mongodb user 생성관리(몽고 db 공식)  (0) 2021.05.27
database dml / ddl / dcl  (0) 2021.05.14

 

 

 

 

 

[DB] DDL, DML, DCL 이란?

명령어 종류 명령어 설명 데이터 조작어 (DML :  Data Manipulation Language SELECT 데이터베이스에 들어 있는 데이터를 조회하거나 검색하기 위한 명령어를 말하는 것으로 RETRIEVE 라고도 함 INSERT UPDATE DE..

brownbears.tistory.com

 

'program > database' 카테고리의 다른 글

entity dao dto 컨셉  (0) 2021.05.27
mongodb user 생성관리(몽고 db 공식)  (0) 2021.05.27
mongodb user 생성 관리  (0) 2021.05.27

+ Recent posts