Total members 11854 |It is currently Fri Jul 30, 2021 2:13 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





You can use @Enumerated , to map java ENUM values to your entity properties .You limit the values to this Enum only.


Entity class :

Code:

package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@
Entity
@Table(name="PERSON")
public class 
Person implements java.io.Serializable{
    
/**
     * 
     */
    
private static final long serialVersionUID 1L;
    @
Id
    
private String Id;
    private 
String name;
    private 
Date birthday;
    @
Enumerated(EnumType.STRING)
    private 
GenderType gender;
    
    @
OneToOne(mappedBy="person"
    private 
Bed bed;
    
    public 
String getId() {
        return 
Id;
    }
    public 
void setId(String id) {
        
Id id;
    }
    @
Column(name="NAME")
    public 
String getName() {
        return 
name;
    }
    public 
void setName(String name) {
        
this.name name;
    }
    @
Column(name="BIRTHDAT")
    public 
Date getBirthday() {
        return 
birthday;
    }
    public 
void setBirthday(Date birthday) {
        
this.birthday birthday;
    }

    public 
GenderType getGender() {
        return 
gender;
    }
    public 
void setGender(GenderType gender) {
        
this.gender gender;
    }
    public 
void setBed(Bed bed) {
    
this.bed bed;
    }
    public 
Bed getBed() {
    return 
bed;
    }
 
    
}

 


Persisting entity :
Code:


package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * @author codemiles.
 */
public abstract class JPAService {
    private 
EntityManager entityManager;

    public 
void insertPerson(String IdString nameDate birthday,
        
GenderType genderBed bed) {
    
EntityManagerFactory factory Persistence
            
.createEntityManagerFactory("persistenceUnitName");
    
entityManager factory.createEntityManager();
    
entityManager.getTransaction().begin();
    
Person person = new Person();
    
person.setName(name);
    
person.setBirthday(birthday);
    
person.setBed(bed);
    
entityManager.persist(person);
    
entityManager.getTransaction().commit();
    
entityManager.close();
    
factory.close();
    }

}


 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : @Enumerated and EnumType.STRING
 Looping through an Enumerated Array     -  
 Elements of the enumerated array are numbers     -  
 Splitting a String Based on a Found String     -  
 recursive string reversal- reverse string     -  
 check if string ends with specific sub-string in php     -  
 check if string start with a specific sub-string in PHP     -  
 String token for string split     -  
 Pad a string to a certain length with another string     -  
 String Flatten (C++)     -  
 formatting string in c++     -  



Topic Tags

Java JPA
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com