buntonn
buntonn

Reputation: 111

How can I get a list of a single field value from an entity?

I am working on a Jhipster app Java service and Angular 5 UI. I have an entity working fine, but I need to get a list of one of the fields (customer) from that entity to display in the UI.

In this case it's a single table I am using which contains the client name, so I am trying to get a distinct list returned for read only.

I have tried creating a custom repository and added a function into the service, Impl class and resource class.

Upon startup its failing with cannot find a property getClientNameList on the entity.

I have show a snippet of the code from the Entity class, the custom repository and the method I added into the PostsServiceImpl class.

Can someone please steer me in the right direction?

Thanks.

// Entity Class //
@Entity
@Table(name = "posts")
public class Posts implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

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

    // Other fields here
    ...
}

// Custom Repository //
@Repository
public interface JobsRepositoryCustom {
    List<String> getClientNameList();
}

// PostsServiceImpl //
public class PostsServiceImpl implements PostsService {

    EntityManager entityManager;

    public List<String> getClientNameList() {

        Query query = entityManager.createNativeQuery("SELECT clientName FROM Posts", Posts.class);
        return query.getResultList();
    }
}

Upvotes: 0

Views: 1256

Answers (1)

pirho
pirho

Reputation: 12205

Your error might be more specifically that clientName is not found. It is not found because if you run a native query you need to use the database column names.

So change:

"SELECT clientName FROM Posts"

to

"SELECT client_name FROM Posts"

Upvotes: 1

Related Questions