Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 981889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:36:46+00:00 2026-05-16T04:36:46+00:00

I’ve been asked to output a CSV file from a view in MySQL. The

  • 0

I’ve been asked to output a CSV file from a view in MySQL. The app I currently am writing uses Spring and Hibernate to create the database, but the view is just handed to me.

Hibernate doesn’t know anything about this view, but I’d want to do something like this:

public List<Object> getCsvView() {
  return (List<Object>) getHibernateTemplate().find("from myView");
}

My guess was that I could map a native query so that hibernate knows about the view. This got a little tricky when I read the docs:

You can also map a native query[…]To
achieve that, you need to describe the
SQL resultset structure using
@SqlResultSetMapping[…].

Now, I’m really not interested in mapping the structure of the result. I’m happy to have the structure just be a bunch of objects.

Furthermore they might change this view at any time. I’m really not thrilled about my app even knowing about the view.

So, is there an easy way to do this in the Spring/Hibernate world, or am I attacking this problem the hard way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:36:46+00:00Added an answer on May 16, 2026 at 4:36 am

    Just perform a native query on the view and you’ll get a List of Object[] with scalar values for each column as result. From the documentation:

    16.1.1. Scalar queries

    The most basic SQL query is to get a
    list of scalars (values).

    sess.createSQLQuery("SELECT * FROM CATS").list();
    sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").list();
    

    These will return a List of Object
    arrays (Object[]) with scalar values
    for each column in the CATS table.
    Hibernate will use ResultSetMetadata
    to deduce the actual order and types
    of the returned scalar values.

    To avoid the overhead of using
    ResultSetMetadata, or simply to be
    more explicit in what is returned, one
    can use addScalar():

    sess.createSQLQuery("SELECT * FROM CATS")
     .addScalar("ID", Hibernate.LONG)
     .addScalar("NAME", Hibernate.STRING)
     .addScalar("BIRTHDATE", Hibernate.DATE)
    

    This query specified:

    • the SQL query string
    • the columns and types to return

    This will return Object arrays, but
    now it will not use
    ResultSetMetadata but will instead
    explicitly get the ID, NAME and
    BIRTHDATE column as respectively a
    Long, String and a Short from the
    underlying resultset. This also means
    that only these three columns will be
    returned, even though the query is
    using * and could return more than the
    three listed columns.

    It is possible to leave out the type
    information for all or some of the
    scalars.

    sess.createSQLQuery("SELECT * FROM CATS")
     .addScalar("ID", Hibernate.LONG)
     .addScalar("NAME")
     .addScalar("BIRTHDATE")
    

    This is essentially the same query as
    before, but now ResultSetMetaData is
    used to determine the type of NAME and
    BIRTHDATE, where as the type of ID is
    explicitly specified.

    How the java.sql.Types returned from
    ResultSetMetaData is mapped to
    Hibernate types is controlled by the
    Dialect. If a specific type is not
    mapped, or does not result in the
    expected type, it is possible to
    customize it via calls to
    registerHibernateType in the
    Dialect.

    Looks perfect to generate a CVS file without knowing anything about the view 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
I am writing an app with both english and french support. The app requests
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.