简体   繁体   中英

Hibernate - How to persist an entity but disregarding certain fields?

I have an Entity with several fields:

@Entity
public class Leilighet {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long leilighetId;
private String navn;
private String beskrivelse;
private String adresse;
private String postNr;
private String postSted;
private double pris;

//The following fields should not be stored:
private String filename;
private String filetype;
private String filesize;
private byte[] filebin;

....
}

I have a corresponding form and Action that will populate this object and persist it to mySql. This is all good when it comes to storing new "Leilighet"-entities.

But when im using this form to "edit" an existing "Leilighet", I have stumbled upon something i cant figure out what to do.

For editing purposes i do not want to load the entire uploaded file. It is enough to just show the filename to indicate that there is a file present. If the user chooses a new File then it should be overwritten, but if the user chooses nothing then the present file should be kept.

But I cant figure out how to make hibernate do what i want. Unless i populate the filebin with the actual file, hibernate will just delete the file.

How can I tell hibernate to just update the other fields and not the filefields?

Hibernate supports loading individual properties lazily. See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#performance-fetching-lazyproperties for details.

Or you could just use a DTO with only the fields you want in this specific use-case, and execute a query with the appropriate projections in order to populate this DTO from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM