Question

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.

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top