1

I'm trying to work a native sql query into an existing entity object while retaining the default Hibernate behavior for the object as a whole.

The majority of the fields on the object are mapped as so:

@Column(name = "FIRST_NAME", nullable = false)
public String getFirstName() {
    return firstName;
}

@Column(name = "MIDDLE_NAME", nullable = true)
public String getMiddleName() {
    return middleName;
}

@Column(name = "LAST_NAME", nullable = false)
public String getLastName() {
    return lastName;
}

@Column(name = "PRIMARY_EMAIL_ADDR", nullable = false)
public String getPrimaryEmailAddress() {
    return primaryEmailAddress;
}

I want to retain this functionality, but add a single field for which I need mapping to a custom (preferably native) SQL Query. I envisioned something like...

Private String foo;
@NativeSQLQuery("SELECT info FROM foo")
public String getFooInfo{return foo}

..but if there is a way to do this as simply as that, I'm missing it.

I've investigated SqlResultSetMapping and several similar Native and Named Query annotations, but all that I can find seems to presume that one is operating at the class level - that the entire entity is mapped in a custom, native fashion, not just one field. How can I go about keeping the normal functionality, but adding the custom mapping for one field? I've done this before using hbm files, I think, quite some time ago, but this current project is annotation-based. At the moment, the best I can come up with is to mark the getter @Transient and to not map it at all, but just use it to execute a query, but I'd really rather not do that, just because it seems like a total hack.

2
  • Is it really that bad to create a new entity for the data you're looking to join with? Or if you're not joining, to just simply fetch it directly from a separate entity? Commented Aug 15, 2013 at 18:05
  • That would make sense, but unfortunately in this case that's not an option. It needs to be integrated with the existing entity, but draw its value from a custom query rather than from a column. Commented Aug 15, 2013 at 18:07

1 Answer 1

1

Try using a @Formula("SELECT info FROM foo"), should works

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.