利用spring mvc,hibernate重构系统(4)--JPA

时间:2021-07-08 23:17:54
POJO 单个主键的
@SuppressWarnings("serial")
@Entity
@Table(name = "CHART_CONFIG")
@SequenceGenerator(name = "CHART_CONFIG_SEQ", sequenceName = "SEQ_CHART_CONFIG_ID" ,allocationSize=1)
public class ChartConfig extends BaseChartConfig {

public ChartConfig (){
super();
}

public ChartConfig (java.lang.Long chartId){
super(chartId);
}

}

 BASE_POJO

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

@MappedSuperclass
public abstract class BaseChartConfig implements Serializable {
@Transient
private int hashCode = Integer.MIN_VALUE;

@Id
@Column(name = "CHART_ID", nullable = false, length = 22, precision = 9, scale = 0)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CHART_CONFIG_SEQ")
private java.lang.Long chartId;

@Column(name = "SERVICE_ID", nullable = true, length = 22, precision = 9, scale = 0)
private java.lang.Long serviceId;

@Column(name = "CHART_NAME", nullable = false, length = 255)
private java.lang.String chartName;

@Column(name = "SERVICE_NAME", nullable = true, length = 255)
private java.lang.String serviceName;

@Column(name = "CHART_XML", nullable = true, length = 4000)
private java.lang.String chartXml;

@Column(name = "CHART_DESC", nullable = true, length = 1024)
private java.lang.String chartDesc;

@Column(name = "STATE", nullable = false, length = 3)
private java.lang.String state;

@Column(name = "STATE_DATE", nullable = false, length = 7)
private java.util.Date stateDate;

@Column(name = "CHART_SWF", nullable = true, length = 255)
private java.lang.String chartSwf;

@Column(name = "SWF_FILE_NAME", nullable = true, length = 255)
private java.lang.String swfFileName;

public java.lang.Long getChartId() {
return chartId;
}

public void setChartId(java.lang.Long chartId) {
this.chartId = chartId;
}

public java.lang.Long getServiceId() {
return serviceId;
}

public void setServiceId(java.lang.Long serviceId) {
this.serviceId = serviceId;
}

public java.lang.String getChartName() {
return chartName;
}

public void setChartName(java.lang.String chartName) {
this.chartName = chartName;
}

public java.lang.String getChartXml() {
return chartXml;
}

public void setChartXml(java.lang.String chartXml) {
this.chartXml = chartXml;
}

public java.lang.String getChartDesc() {
return chartDesc;
}

public void setChartDesc(java.lang.String chartDesc) {
this.chartDesc = chartDesc;
}

public java.lang.String getState() {
return state;
}

public void setState(java.lang.String state) {
this.state = state;
}

public java.util.Date getStateDate() {
return stateDate;
}

public void setStateDate(java.util.Date stateDate) {
this.stateDate = stateDate;
}

public java.lang.String getChartSwf() {
return chartSwf;
}

public void setChartSwf(java.lang.String chartSwf) {
this.chartSwf = chartSwf;
}

public java.lang.String getSwfFileName() {
return swfFileName;
}

public void setSwfFileName(java.lang.String swfFileName) {
this.swfFileName = swfFileName;
}

// constructors
public BaseChartConfig() {
initialize();
}

/**
* Constructor for primary key
*/
public BaseChartConfig(java.lang.Long chartId) {
this.setChartId(chartId);
initialize();
}

protected void initialize() {
}

public boolean equals(Object obj) {
if (null == obj)
return false;
if (!(obj instanceof BaseChartConfig))
return false;
else {
BaseChartConfig mObj = (BaseChartConfig) obj;
if (null == this.getChartId() || null == mObj.getChartId())
return false;
else
return (this.getChartId().equals(mObj.getChartId()));
}
}

public int hashCode() {
if (Integer.MIN_VALUE == this.hashCode) {
if (null == this.getChartId())
return super.hashCode();
else {
String hashStr = this.getClass().getName() + ":"
+ this.getChartId().hashCode();
this.hashCode = hashStr.hashCode();
}
}
return this.hashCode;
}

public String toString() {
return super.toString();
}

public void setServiceName(java.lang.String serviceName) {
this.serviceName = serviceName;
}

public java.lang.String getServiceName() {
return serviceName;
}

}

 复合主键的POJO:

import javax.persistence.Entity;
import javax.persistence.Table;

import com.linkage.report.model.base.BaseRptForm;

@SuppressWarnings("serial")
@Entity
@Table(name = "RPT_FORM")
public class RptForm extends BaseRptForm {

public RptForm() {
super();
}

public RptForm(RptFormPK rptFormPK) {
super(rptFormPK);
}

}

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import com.linkage.report.model.RptFormPK;

@MappedSuperclass
public abstract class BaseRptForm implements Serializable {
@Transient
private int hashCode = Integer.MIN_VALUE;

@EmbeddedId
private RptFormPK rptFormPK;

@Column(name = "ORDER_ID", nullable = false, length = 22, precision = 9, scale = 0)
private java.lang.Long orderId;

@Column(name = "STATE", nullable = false, length = 3)
private java.lang.String state;

public RptFormPK getRptFormPK() {
return rptFormPK;
}

public void setRptFormPK(RptFormPK rptFormPK) {
this.rptFormPK = rptFormPK;
}

public java.lang.Long getOrderId() {
return orderId;
}

public void setOrderId(java.lang.Long orderId) {
this.orderId = orderId;
}

public java.lang.String getState() {
return state;
}

public void setState(java.lang.String state) {
this.state = state;
}

// constructors
public BaseRptForm() {
initialize();
}

/**
* Constructor for primary key
*/
public BaseRptForm(RptFormPK rptFormPK) {
this.setRptFormPK(rptFormPK);
initialize();
}

protected void initialize() {
}

public boolean equals(Object obj) {
if (null == obj)
return false;
if (!(obj instanceof BaseRptForm))
return false;
else {
BaseRptForm mObj = (BaseRptForm) obj;
if (null == this.getRptFormPK() || null == mObj.getRptFormPK())
return false;
else
return (this.getRptFormPK().equals(mObj.getRptFormPK()));
}
}

public int hashCode() {
if (Integer.MIN_VALUE == this.hashCode) {
if (null == this.getRptFormPK())
return super.hashCode();
else {
String hashStr = this.getClass().getName() + ":"
+ this.getRptFormPK().hashCode();
this.hashCode = hashStr.hashCode();
}
}
return this.hashCode;
}

public String toString() {
return super.toString();
}

}

import javax.persistence.Embeddable;

@Embeddable
public class RptFormPK extends BaseRptFormPK {

public RptFormPK (){
super();
}
}


@MappedSuperclass
public abstract class BaseRptFormPK implements Serializable {

@Column(name = "RPT_ID", nullable = false, length = 22, precision = 9, scale = 0)
private java.lang.Long rptId;

@Column(name = "ITEM_ID", nullable = false, length = 256)
private java.lang.String itemId;

public java.lang.Long getRptId() {
return rptId;
}

public void setRptId(java.lang.Long rptId) {
this.rptId = rptId;
}

public java.lang.String getItemId() {
return itemId;
}

public void setItemId(java.lang.String itemId) {
this.itemId = itemId;
}

// constructors
public BaseRptFormPK() {
initialize();
}

protected void initialize() {
}

public String toString() {
return super.toString();
}

}

1.---表名

@Entity
@Table(name = "RPT_FORM") 指向表名RPT_FORM

 

2.---主键规则

@SequenceGenerator(name = "CHART_CONFIG_SEQ", sequenceName = "SEQ_CHART_CONFIG_ID" ,allocationSize=1) --主键生成序列SEQ_CHART_CONFIG_ID

@Id
 @Column(name = "CHART_ID", nullable = false, length = 22, precision = 9, scale = 0)
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CHART_CONFIG_SEQ")
 private java.lang.Long chartId;

 

3.POJO属性和table字段对应设置 --serviceId对应SERVICE_ID

@Column(name = "SERVICE_ID", nullable = true, length = 22, precision = 9, scale = 0)
 private java.lang.Long serviceId;

 

4.hibernate在spring如何加载这些注解信息?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>WEB-INF/classes/com/</value>
</list>
</property>
<property name="packagesToScan" >
<list>
<value>com.**.*</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_outer_join">true</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.connection.pool_size">10</prop>
<prop key="hibernate.jdbc.fetch_size">100</prop>
</props>
</property>
<property name="lobHandler">
<ref bean="oracleLobHandler"/>
</property>
</bean>

<!-- oralce blobHandler -->
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" lazy-init="true"/>
<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
<property name="nativeJdbcExtractor">
<ref bean="nativeJdbcExtractor"/>
</property>
</bean>

</beans>