0

私はSpringブートレストAPIに取り組んでおり、pojoクラスを結果セットにマッピングすることについて以前に質問し ましたクラスで適切なコンストラクターを見つけることができませんでした.....

私が得た答えは効果的に機能しましたが、1 つのエンティティ クラスと 1 つの SqlResultSetMapping ファイルのみを使用している場合です。

私のスタックトレース:

javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown SqlResultSetMapping [UserMapping]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.AbstractSharedSessionContract.createNativeQuery(AbstractSharedSessionContract.java:1035)
    at org.hibernate.internal.AbstractSharedSessionContract.createNativeQuery(AbstractSharedSessionContract.java:109)
    at com.app.ecclesiamainframe.service.impl.UsersServiceImpl.findByUsername(UsersServiceImpl.java:89)
    at com.app.ecclesiamainframe.service.impl.UsersServiceImpl$$FastClassBySpringCGLIB$$ae026741.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)
Caused by: org.hibernate.MappingException: Unknown SqlResultSetMapping [UserMapping]
    at org.hibernate.query.internal.NativeQueryImpl.setResultSetMapping(NativeQueryImpl.java:147)
    at org.hibernate.internal.AbstractSharedSessionContract.createNativeQuery(AbstractSharedSessionContract.java:1031)
    ... 64 more

私のメンバーエンティティ:

 * 
 */
package com.app.ecclesiamainframe.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;

import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import com.vladmihalcea.hibernate.type.json.JsonStringType;

import lombok.Data;

/**
 * @author Harry
 *
 */
@Entity
@Table(name="members_tb")
@Data
@TypeDef(
    name = "json", 
    typeClass = JsonStringType.class
)
@SqlResultSetMapping(
    name = "MemberMapping",
classes = @ConstructorResult(
    targetClass = Members.class,
    columns = {
        @ColumnResult(name = "memberId", type = Long.class),
        @ColumnResult(name = "dcaRegno", type = Long.class),
        @ColumnResult(name = "address"),
        @ColumnResult(name = "age"),
        @ColumnResult(name = "areaId", type = Long.class),
        @ColumnResult(name = "birthday"),
        @ColumnResult(name = "cellId", type = Long.class),
        @ColumnResult(name = "dca"),
        @ColumnResult(name = "department"),
        @ColumnResult(name = "editor"),
        @ColumnResult(name = "firstTime"),
        @ColumnResult(name = "gender"),
        @ColumnResult(name = "maritalStatus"),
        @ColumnResult(name = "memberEmail"),
        @ColumnResult(name = "memberHomePhoneNum", type = Long.class),
        @ColumnResult(name = "memberMobileNum", type = Long.class),
        @ColumnResult(name = "memberName"),
        @ColumnResult(name = "note"),
        @ColumnResult(name = "secondTime")

    }))
public class Members implements Serializable {

    /**
 * 
 */
public Members() {}

private static final long serialVersionUID = 1L;

@Id
@Column(name="memberId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long memberId;

@Column(name="dcaRegno")
private Long dcaRegno;

@Type(type = "json")
@Column(columnDefinition = "json",name="address")
private String address;

@Column(name="age")
private String age;

@Column(name="areaId")
private Long areaId;

@Column(name="birthday")
private String birthday;

@Column(name="cellId")
private Long cellId;

@Type(type = "json")
@Column(columnDefinition = "json",name="dca")
private String dca;

@Column(name="department")
private String department;

@Column(name="editor")
private String editor;

@Column(name="firstTime")
private String firstTime;

@Column(name="gender")
private String gender;

@Column(name="maritalStatus")
private String maritalStatus;

@Column(name="memberEmail")
private String memberEmail;

@Column(name="memberHomePhoneNum")
private Long memberHomePhoneNum;

@Column(name="memberMobileNum")
private Long memberMobileNum;

@Column(name="memberName")
private String memberName;

@Column(name="note")
private String note;

@Column(name="secondTime")
private String secondTime;

public Members(Long memberId,String memberName) {
    this.memberId = memberId;
    this.memberName = memberName;
}

public Members(Long memberId, Long dcaRegno, String address, String age, Long areaId, String birthday, Long cellId, String dca,
        String department, String editor, String firstTime, String gender, String maritalStatus, String memberEmail, 
        Long memberHomePhoneNum, Long memberMobileNum, String memberName, String note, String secondTime) {

    this.memberId = memberId;
    this.dcaRegno = dcaRegno;
    this.address = address;
    this.age = age;
    this.areaId = areaId;
    this.birthday = birthday;
    this.cellId = cellId;
    this.dca = dca;
    this.department = department;
    this.editor = editor;
    this.firstTime = firstTime;
    this.gender = gender;
    this.maritalStatus = maritalStatus;
    this.memberEmail = memberEmail;
    this.memberHomePhoneNum = memberHomePhoneNum;
    this.memberMobileNum = memberMobileNum;
    this.memberName = memberName;
    this.note = note;
    this.secondTime = secondTime;
}

}

私のユーザーエンティティ:

 * 
 */
package com.app.ecclesiamainframe.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;

//import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
//import org.springframework.security.authentication.encoding.PasswordEncoder;

import lombok.Data;

/**
 * @author Harry
 *
 */
@Entity
@Table(name="users_tb")
@Data

@SqlResultSetMapping(
    name = "UserMapping",
classes = @ConstructorResult(
    targetClass = Users.class,
    columns = {
        @ColumnResult(name = "userId", type = Long.class),
        @ColumnResult(name = "username"),
        @ColumnResult(name = "password"),
        @ColumnResult(name = "name"),
        @ColumnResult(name = "areaId", type = Long.class),
        @ColumnResult(name = "permission")
    }))
public class Users implements Serializable {

    /**
 * 
 */
 public Users() {}

 private static final long serialVersionUID = 1L;

    @Id
    @Column(name="userId")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long userId;

    @Column(name="username")
    private String username;

    @Column(name="password")
    private String password;

    @Column(name="name")
    private String name;

    @Column(name="areaId")
    private Long areaId;

    @Column(name="permission")
    private String permission;


//  public String getPassword() {
//      return password;
//  }

//  public void setPassword(String password) {
//      PasswordEncoder crypto = new Md5PasswordEncoder();
//      this.password = crypto.encodePassword(password, null);
//  }

    public Users(Long userId, String username,String password, String name, Long areaId, String permission) {

        this.userId = userId;
        this.username = username;
        this.password = password;
        this.name = name;
        this.areaId = areaId;
        this.permission = permission;
    }

}

私の MemberServiceImpl:

 * 
 */
package com.app.ecclesiamainframe.service.impl;

import java.util.List;
import java.util.Optional;

import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;

import org.hibernate.Session;
import org.hibernate.Transaction;

import org.springframework.stereotype.Service;

import com.app.ecclesiamainframe.dao.MembersDao;
import com.app.ecclesiamainframe.entity.Members;
import com.app.ecclesiamainframe.service.MembersService;
import com.app.ecclesiamainframe.util.HibernateUtil;
/**
 * @author Harry
 *
 */
@Service
public class MembersServiceImpl implements MembersService {
    Session session = null;
    Transaction transaction = null;
    @Autowired
    private MembersDao membersDao;

    public MembersServiceImpl() {
        super();
    }

    @Transactional
    public Members saveMember(Members member) {
        // TODO Auto-generated method stub
        return membersDao.save(member);
    }

    @Transactional
    public Members updateMembers(Members member) {
        // TODO Auto-generated method stub
        return membersDao.saveAndFlush(member);
    }

                @Transactional
                @SuppressWarnings("unchecked")
                public List<Members> findByMembername(String memberName) 
                {
                    //return membersDao.findByMemberName(memberName);
                     List<Members> members = null;
                    try {
                         session = HibernateUtil.getSessionFactory().openSession();
                         transaction = session.beginTransaction();

                      // Native query selecting all columns
                         Query query = session.createNativeQuery("SELECT * FROM members_tb where memberName like :memberName","MemberMapping")
                                 .setParameter("memberName","%"+memberName+"%"); //named parameter binding 
                                members = query.getResultList();
                         transaction.commit(); 
                      } catch (Exception e) {
                         e.printStackTrace();
                      } finally {
                         if (session != null) {
                            session.close();
                         }
                      }
                     // HibernateUtil.shutdown();
                      return members;
                }   

    @Transactional
    public List<Members> getMembers() {
        // TODO Auto-generated method stub
        return membersDao.findAll();
    }

    @Transactional
    public Optional<Members> getMember(Long memberId) {
        // TODO Auto-generated method stub
        return membersDao.findById(memberId);
    }

    @Transactional
    public void deleteMember(Long memberId) {
        // TODO Auto-generated method stub
        membersDao.deleteById(memberId);
    }

}


私の UserServiceImpl:

         * 
         */
        package com.app.ecclesiamainframe.service.impl;

        import java.util.ArrayList;
        import java.util.Collection;
        import java.util.List;
        import java.util.Optional;

        import javax.persistence.Query;
        import javax.transaction.Transactional;

        import org.springframework.beans.factory.annotation.Autowired;

        import org.hibernate.Session;
        import org.hibernate.Transaction;

        //import org.primefaces.component.inputtext.InputText;
        //import org.springframework.security.core.GrantedAuthority;
        //import org.springframework.security.core.authority.SimpleGrantedAuthority;
        //import org.springframework.security.core.userdetails.User;
        //import org.springframework.security.core.userdetails.UserDetails;
        //import org.springframework.security.core.userdetails.UsersService;
        //import org.springframework.security.core.userdetails.UsernameNotFoundException;


        import org.springframework.stereotype.Service;

        import com.app.ecclesiamainframe.dao.UsersDao;
        import com.app.ecclesiamainframe.entity.Users;
        import com.app.ecclesiamainframe.service.UsersService;
        import com.app.ecclesiamainframe.util.HibernateUtil;

        /**
         * @author Harry
         *
         */
        @Service
        public class UsersServiceImpl implements UsersService {
            Session session = null;
            Transaction transaction = null;
            @Autowired
            private UsersDao usersDao;

            public UsersServiceImpl() {
                super();
            }

            @Transactional
            public Users saveUser(Users user) {
                // TODO Auto-generated method stub
                return usersDao.save(user);
            }

            @Transactional
            public Users updateUser(Users user) {
                // TODO Auto-generated method stub
                return usersDao.saveAndFlush(user);
            }

            @Transactional
            public List<Users> getUsers() {
                // TODO Auto-generated method stub
                return usersDao.findAll();
            }

            @Transactional
            public Optional<Users> getUser(Long userId) {
                // TODO Auto-generated method stub
                return usersDao.findById(userId);
            }

            @Transactional
            public void deleteUser(Long userId) {
                // TODO Auto-generated method stub
                usersDao.deleteById(userId);
            }

            //@SuppressWarnings("unchecked")
            @Transactional
            public Users findByUsername(String userName) {
                //return usersDao.findByMemberName(memberName);
                 Users users = null;
                try {
                     session = HibernateUtil.getSessionFactory().openSession();
                     transaction = session.beginTransaction();
                  // Native query selecting all columns
                     Query query = session.createNativeQuery("SELECT * FROM users_tb where username like :userName","UserMapping")
                             .setParameter("userName","%"+userName+"%"); //named parameter binding 
                     users = (Users) query.getSingleResult();
                     transaction.commit(); 
                  } catch (Exception e) {
                     e.printStackTrace();
                  } finally {
                     if (session != null) {
                        session.close();
                     }
                  }
                 // HibernateUtil.shutdown();
                  return users;
            }

私の質問は

メンバー、ユーザー、コースなど、いくつかのエンティティがあり、各クラスの SqlResultSetMapping ファイルを設計しました。しかし、API のみをテストすると、メンバー クラスは正しい応答とマッピングを取得し、ユーザー エンティティ サービスの実装は不明な SqlResultSetMapping [UserMapping] を返します...

4

0 に答える 0